页面位置更改时调用Tampermonkey脚本 [英] Recall Tampermonkey script when page location changes
问题描述
好吧,我想知道当用户更改位置时是否可以调用Tampermonkey脚本(但匹配仍处于活动状态)。例如,我的脚本挂钩youtube网站。
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);
如你所见,我调用console.log进行调试,当我刷新或调用它时会调用它我是第一次加载网页。但有一次我改变了视频,它不再被调用,这就是我想要避免的。
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.
我还审查了这个: http://tampermonkey.net/documentation.php 我找不到任何东西,也许我的评论太快了?
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?
推荐答案
使用自定义 YouTube SPF活动:
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") });
提示Chrome用户查找此类活动:
使用DevTools => Elements panel =>事件监听器窗格
Tip for Chrome users to find such events:
Use DevTools => Elements panel => Event Listeners pane
在较新版本的Chrome上:
使用DevTools => Sources面板=>事件监听器(不是断点)
On newer versions of Chrome:
Use DevTools => Sources panel => Event Listeners (not breakpoints)
这篇关于页面位置更改时调用Tampermonkey脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!