Recall Tampermonkey script when page location changes

跟風遠走 提交于 2019-11-26 11:36:35

问题


Well, I want to know if its possible to recall a Tampermonkey script when a user changes his location (but the match is still active). For example, my scripts hooks youtube website.

I need to make that the script recalls itself when I change the video, my actual script is:

// ==UserScript==
// @name        xxx
// @namespace    xxx
// @version      1.0
// @description  xxx
// @author       Ikillnukes
// @match        https://www.youtube.com/*
// @match        https://youtu.be/*
// @grant        none
// ==/UserScript==

console.log(\"Tampermonkey hook!\");
var script = document.createElement(\'script\');
script.src = document.location.protocol+\"//xxx\";
(document.body || document.head || document.documentElement).appendChild(script);

As you can see I call console.log for debug it, and it gets called when I refresh or I load the webpage for the first time. But one time I change the video it doesn\'t get called anymore, and that is what I want to avoid.

I also reviewed this: http://tampermonkey.net/documentation.php and I didn\'t find anything, maybe I reviewed it too quickly?

So, any suggestions there?


回答1:


Use custom YouTube SPF events defined by the youtube script:

window.addEventListener("spfrequest", function(e) { console.log("requesting new page") });
window.addEventListener("spfprocess", function(e) { console.log("new page is processed") });
window.addEventListener("spfdone", function(e) { console.log("new page is displayed") });

Tip for Chrome users to find such events:
Use DevTools => Elements panel => Event Listeners pane

On newer versions of Chrome:
Use DevTools => Sources panel => Event Listeners (not breakpoints)



来源:https://stackoverflow.com/questions/32275387/recall-tampermonkey-script-when-page-location-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!