【问题标题】:JavaScript, Tampermonkey GM_setValue and GM_getValueJavaScript、Tampermonkey GM_setValue 和 GM_getValue
【发布时间】:2016-06-12 15:58:58
【问题描述】:

我有两个不同的脚本。在结束时,它会打开一个新网页,该网页启动另一个脚本,然后等待该脚本将值设置为 true 以允许第一个脚本打开另一个页面。

这是在第一个脚本末尾调用的函数的脚本。它等待标志值 continueValue 设置为 true。控制台反复显示“检查标志”消息,暗示其他脚本没有将值更改为 true。

function checkFlag() {
    console.log("Checking flag");
    if(GM_getValue("continueValue") === false) {
       window.setTimeout(checkFlag, 2000); /* this checks the flag every 3000 milliseconds*/
    } else {
        //--- Opens the next cove
        if (!(currentCoveNum = -1)) {
            window.open(coveLinks[currentCoveNum + 1],"_self");
        }
    }
}

在下一个脚本结束时,执行以下代码:

//--- If the text "Next Creature" exists on the page, click next creature button
if ((document.documentElement.textContent || document.documentElement.innerText).indexOf('Next Creature') > -1) {
  window.location.href = nextCreatureLink[0].href;
//--- Otherwise set the continue value to true to allow Super Auto Feed, Open Coves to open the next cove
} else {
    GM_setValue("continueValue", true);
    console.log("continueValue is "+GM_getValue("continueValue"));
    setTimeout(function() {
        window.close();
    }, (2 * 1000));
}

问题是,当此脚本到达消息“continueValue is”时,它显示为 true,这意味着其他脚本应该打开下一页,但事实并非如此。它只是不断检查标志是否变为真。

我想知道 getValue 和 setValue 可能在脚本之间不起作用?或者,检查标志是否为真的循环可能是错误的。

如果有人能告诉我我的脚本哪里错了,我会非常感激。

【问题讨论】:

    标签: javascript greasemonkey tampermonkey setvalue getvalue


    【解决方案1】:

    GM_setValue 和 GM_getValue 确实在同一脚本中运行交叉表或窗口,但它们不运行交叉脚本。因此,当在脚本中设置值时,它不会更改其他脚本的值。

    尝试本地存储或存储在外部数据库中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多