Super simple script runs on some pages, not others. Why? - javascript

I have a super simple script (running in a Userscript manager for Safari) that works on some pages but not all.
Here is the script:
// ==UserScript==
// #name Close tab on double click
// #include https://*
// ==/UserScript==
document.addEventListener("dblclick",function (event) {
window.close();
});
For some reason, it works on Stack Overflow, Reddit post pages, etc., but does NOT work on Reddit homepage, Google (homepage or search results), etc.
Why?
Thanks for any insight. I'm still a newbie about javascript.
Edit: Working now, using the GM.closeTab permission from my Userscript manager, as documented here: https://github.com/quoid/userscripts#api

Try using
// #grant window.close
in order to use window.close per the documentation.

In a chrome browser, when I add the JS function in the console and then double click on the page I get this notification (I tried the stack overflow site):
Console Image
My guess is your userscript manager may add the script you write differently depending on the website, and the websites that don't work are because of this warning. Try opening your console after you add the script and see if you get this warning when double clicking does not work.

Related

TamperMonkey not running scripts only when loading youtube's homepage

I made a short script that makes that removes the 'play on tv' button on youtube, and the script itself works totally fine.
The problem is that the script doesn't even run at all when I start on youtube's homepage. And because of how youtube runs, when you click on a video, it continues to not run the script at all. But if I then refresh on the video page, the script starts running fine, even if I go back to the homepage then onto a video again(as long as it is the same tab). But then it will also stop working again if I refresh on the homepage. It also works totally fine when if I open a new tab on youtube directly onto a video. It just doesn't run when a new session of youtube is started on a tab specifically on the homepage.
I tried using these:
#match https://www.youtube.com/*
#include https://www.youtube.com
But the script still does not run at all.
I started to think this may be a bug on TamperMonkey's side because it isn't even only my script that seems to have this issue. I have one more script that runs on youtube, Youtube Auto-Translate Canceler, that also seems to be running into this issue as well.
It is also bizzare that when you load onto the youtube homepage, TamperMonkey on the extension bar seems to show that there are 2 scripts running for a split second before it disappears and shows 'no scripts is running'.
As I was writing this, I tested these out:
#match https://*
#match https://*/*
And it still didn't work only for youtube's homepage.
I did see this other question that is maybe running into a similar situation, but I'm not too sure if this is the same problem, because to me it seems like if this is the case then it should not work for all of youtube, not just the homepage.
So I am now pretty convinced that this is a most likely a bug. But I do want to know if anyone knows any kind of work arounds if there are any. It may also have to do with google chrome specifically, but I'm not sure.
Thank you.

How to run user scripts on Firefox's built-in PDF Viewer?

I'm running a simple Greasemonkey/Tampermonkey user scrip in Firefox which gets selection of a text (let's say, just a single word) and opens a translator for it.
// ==UserScript==
// #name translator
// #version 1.0
// ==/UserScript==
document.addEventListener('dblclick', handleDblClick, true);
function handleDblClick(e) {
var txt = window.getSelection().toString();
window.open("https://translate.google.ru/?hl=ru&text=" + txt);
}
This works fine with ordinary web-pages, but not with built-in PDF Viewer. Is there any chance to make this (or any other) script running while viewing PDF-files in Firefox?
Here is a small sample of PDF-file to try with: http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf
I have a similiar problem and came to the conclusion that your desired behaviour is unfortunately not possible after Firefox 60. However, bookmarklets work in the built-in PDF viewer. One possible fix would be to rewrite your userscript to work as a bookmarklet.
Here is a bug report indicating that the current behaviour is intentional: https://bugzilla.mozilla.org/show_bug.cgi?id=1454760
Good luck!

How to debug non-dynamic script after dynamic script loads on the same page

This answer https://stackoverflow.com/a/10929430/749227
to this question Is possible to debug dynamic loading JavaScript by some debugger like WebKit, FireBug or IE8 Developer Tool? is spot on for debugging dynamic scripts.
The issue I am facing is that I have a page that has a script on it, and after it loads an ajax request fires which returns with some HTML and a script that get put into the page. With the //# sourceURL=myDynamicDocumentFragment.html bit added, I can debug the dynamic script just fine.
But once it's loaded, then the other script that is part of the outer page that initially loaded goes off the rails. I can set breakpoints on blank lines and can't set them on legitimate lines. The debugger will stop on them but it won't be at the place in the code where I'd expect.
What it appears to be is that the dev tools window is showing the original script, and the debugger itself is running on something else - some updated version of code that includes both the outer page's script and the dynamic script that was added later. Or maybe it just hiccups with respect to line numbers it's displaying and what those map to in the code it's actually running.
I wish I had a good simple code snippet to demonstrate the issue, but I don't. Has anyone seen this, and does anyone know of a way to have Chrome 'refresh' the dev tools scripts/debugger without refreshing the page? (it has to be w/o refreshing the page since things work fine when the page loads - it's only after the dynamic script is dropped in that the wheels come off)
Note: I've tagged with Chrome since that's what I'm using (v 38). I don't know how other browsers fare.
You can find scripts injected into head or evaluated, here is a break point added on youtube evaluated (another js file).
You can find this in chrome as well, adding console.log (click on message shown), and voila you have source code you can add break points.
Here mozila print debugging/breakpoint over evaluated script on utube page:
Update
Sorry, I understand chrome was out of the scope, my engrish :)
How I did debugging on chrome over injected scripts, but there are cases when you cannot attach to execution if script is active (page load plus few milliseconds), you need to search for workarounds.
Added this at the begin of the script injected:
//# sourceURL=jseinjectedsource.js
console.log("evaluated");
and voila console:
Better check this way better than my explanation chrome developer
Check to see if your script is using a source map (if you're using TypeScript this is typically on by default for VS projects).
I've found Chrome to be really bad with source maps, often refusing to update them, or stop displaying them after the source map line is removed from the code.

Chrome DevTools script blackboxing not working

I am trying to use the new feature of chrome devtools "blackboxing a script".
This Chrome Devtools article lists script blackboxing functionality
What happens when you blackbox a script?
Exceptions thrown from library code will not pause (if Pause on
exceptions is enabled)
Stepping into/out/over bypasses the library code
Event listener breakpoints don't break in library code
The debugger will not pause on any breakpoints set in library code.
The end result is you are debugging your application code instead of third party resources.
I have tried to blackbox the file directly by right clicking the file from source tab and everything goes right the yellow notification shows at the top of file "This script is blackboxed in debugger". Strangely none of the above listed points work, debugger goes through blackboxed script also event listeners reference the blackboxed script. I have also enabled Developer Tools experiments at chrome://flags/
With these options set i thought i would have been ok, i also have the latest chrome at this time 39.0 beta,i have no idea what i'm missing. Did anybody go through this?
Thnx!
I had the same issue. One quick and easy way is to look at your Call Stack. As a for instance, Right-Click on one of the 'jquery' functions and select 'black box' from the menu. Nothing else needed. In this example jquery.js will be step-over from that point on.
In your screenshot, I can see a breakpoint.
If you put a breakpoint, it will always break (except if you deactivate it, of course), even with blackboxing being active.
Had the same issue and used good old
have you tried turning it off and on again?
Go to developer tools settings -> Blackboxing -> Remove all entries one by one + disable Blackbox content scripts
Blackboxed again and after that this works fine.

iframe undefined in Greasemonkey script

I have created a Greasemonkey script that runs fine in the firebug editor, with the Greasemonkey specifics removed, but not when I try to package it as a userscript. The Firefox error console is reporting that an iframe I am trying to use is undefined.
I've cut the userscript down to a minimum case where it should be printing the iframe html into the firebug console, and does when run in the firebug editor, but is not working as a userscript:
// ==UserScript==
// #name Movies
// #include http://*.princecharlescinema.com/*
// #include http://princecharlescinema.com/*
// ==/UserScript==
// stop script loading multiple times
if (top !=self) return;
var iframeHTML = window.frames['iframe2'].document.documentElement.innerHTML;
unsafeWindow.console.log(iframeHTML);
An example page the script is intended for
If it's of any use the gist of the full script is I collect all the td tags from this iframe, get some information from them, and then insert some new html into some of the same td tags.
Any help would be appreciated.
Perhaps you need to wait for DOMFrameContentLoaded

Categories