سؤال

When I make a chrome extension for youtube with userscript in Tampermonkey or as a Chrome Extension the extension doesn't recognize when it leaves the page that the extension should run on like it normally would because of the history.push function that has been implemented recently. So for example:

// @include     http://www.youtube.com/feed/subscriptions

I did this so it should only run when I go to my subscriptions but when I click a video it keeps loading the extension. When I turn it into a chrome extension the problem persists.

How can I see if it is on a certain youtube-page to then run my code?

هل كانت مفيدة؟

المحلول

The problem is caused by pages that modify the URL by history.pushState(). It only modifies the display URL and the contents without a page reload.

So when you run a script at Tampermonkey you need to force a reload on that manipulation.

Note: if you only want to make the script not run when you navigate away from the subscriptions page then just change the @match statement to your @include.

// ==UserScript==
// @name       YT pushState
// @namespace  http://tampermonkey.net/
// @version    0.1
// @match      *://*.youtube.com/*
// @grant      none
// ==/UserScript==

window.history.__proto__.pushState = function(a, b, url) {
    window.location.href = url;
}

if (window.location.href.match(/https?:\/\/www\.youtube\.com\/feed\/.*/)) {
    console.log("Put main function here!");                               
} else {
    console.log("Ignore this page");
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top