Tampermonkey,Userscripts使用POP UP向脚本添加URL

问题描述 投票:2回答:1

我正在使用tampermonkey在网站上运行一些脚本。

现在我试图让你去任何一个页面(“example.com”)

你会弹出一个你可以按是或否的弹出窗口。

是=将URL添加到tampermonkey脚本用户包含(脚本将在此URL上运行)否=不添加

无法在互联网上找到关于这个主题的文档,所以我想知道是否有人有任何建议?

javascript jquery greasemonkey tampermonkey
1个回答
0
投票

您将不得不解决一个变通方法,因为我认为您不能动态添加规则,即要包含的URL。

将以下内容添加到用户脚本标头中。

// @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 ...
© www.soinside.com 2019 - 2024. All rights reserved.