我正在使用 tampermonkey 在网站上运行一些脚本。
现在我试图让你每次进入某个页面(“example.com”)
您将看到一个弹出窗口,您可以在其中按"is"或“否”。
是 = 将 URL 添加到用户包含的 tampermonkey 脚本中(该脚本将在此 URL 上运行) 否 = 不添加
在互联网上找不到有关此主题的文档,因此我想知道是否有人有任何建议?
最佳答案
您必须找到解决方法,因为我认为您无法动态添加规则,即要包含的 URL。
将以下内容添加到您的用户脚本 header 。
// @match *
// @grant GM_setValue
// @grant GM_getValue
以下代码将包含一个 URL。
function includeUrl(url) {
var includes = JSON.parse(GM_getValue('includes')) || [];
includes.push(url);
GM_setValue('includes', JSON.stringify(includes));
}
以下代码将确定脚本是否应运行。
function shouldRun() {
var includes = JSON.parse(GM_getValue('includes'));
if (includes) {
var url = window.location.href;
if (includes.indexOf(url) > -1) {
return true;
}
}
return false;
}
将它们缝合在一起
// prompt user if they want to include Url (use includeUrl)
if (!shouldRun()) return; // prevent execution
// your code ...
关于javascript - Tampermonkey,用户脚本使用 POP UP 将 URL 添加到脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29927599/