Search code examples
javascriptsafariuserscripts

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


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


Solution

  • 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.