TamperMonkey namespacing will not scope to top level domain

1.4k views Asked by At

I would like to run this TamperMonkey script on all YouTube.com sites exclusively:

// ==UserScript==
// @name         YouTubeFakeScriptName
// @version      0.3
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant        GM_addStyle
// ==/UserScript==

// Insert any JavaScript here.

Here's the problem, it's not scoped to YouTube only. I have tried many alternatives:

// @namespace    http://youtube.com
// @namespace    https://youtube.com
// @namespace    https://youtube.com/
// @namespace    https://youtube.com/*
// @namespace    youtube.com
// @namespace    *
// @namespace    */youtube.com/*   

Similarly with @include, here is what it said on http://tampermonkey.net/documentation.php The pages on that a script should run. Multiple tag instances are allowed. Please note that @include doesn't support the URL hash parameter. Please visit this forum thread for more information: click. Code:

// @include http://tampermonkey.net/*
// @include http://*
// @include https://*
// @include *

Along with many other possibilities, but I cannot seem to make scope only apply to YouTube, I can either:

  • apply all TamperMonkey code to all websites
  • apply no TamperMonkey code to all websites
  • Have TamperMonkey script appear when extension is pressed
    • And be Green
    • And be Red
  • Have TamperMonkey script not appear when extension is pressed

This last option is even odder, using match has strange behavior; I cannot even get my script to even appear when clicking the extension button if I use either:

// @match        https://youtube.com/*
// @match        *

But the script does appear if I don't use @match at all or use:

// @match        */*

Additional: I can confirm that using the following produces no script in the extension upon clicking Tampermonkey:

// ==UserScript==
// @name         YouTubeRandomAppHere
// @match        https://youtube.com/*
// @version      0.3
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant        GM_addStyle
// ==/UserScript==

The solution required a few steps which are added to the original post at the bottom, so that others may learn as well. Thank you for your help.

remove @namespace add both:

// @match       *://*.youtube.com/*
// @noframes
2

There are 2 answers

5
Brock Adams On BEST ANSWER

Note:

  1. The best way to specify sites in Tampermonkey is with the @match directive. (Or use @include for more options but less performance and "safety".)

  2. YouTube almost always uses the www. subdomain, so directives like
    // @match https://youtube.com/*
    will almost never match.

  3. Occasionally, http:// still works.

  4. From your description, you probably don't want the script to work on iframed or embedded content.

So you want directive(s) that match the following URL patterns and exclude everything else:

https://www.youtube.com/*
http://www.youtube.com/*
https://youtube.com/*
http://youtube.com/*

See the Match Patterns documentation. The pattern that does all that is:

// @match  *://*.youtube.com/*

Putting it all together:

// ==UserScript==
// @name        YouTubeRandomAppHere
// @match       *://*.youtube.com/*
// @version     0.3
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant       GM_addStyle
// @noframes
// ==/UserScript==

alert ("Hello World!");
3
tlehman On

Change

// @namespace    https://youtube.com

to

// @match        https://youtube.com/*