2016-06-12 66 views
1

我有兩個不同的腳本。在一個結尾處,它會打開一個新的網頁,啓動另一個腳本,然後等待該腳本將值設置爲true,以允許第一個腳本打開另一個頁面。JavaScript,Tampermonkey GM_setValue和GM_getValue

這是在第一個腳本末尾調用的函數的腳本。它等待標誌值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是」它顯示屬實,這意味着其他腳本應該打開下一頁,但它沒有。它只是不斷檢查國旗是否成真。

我想知道也許getValue和setValue不能在腳本之間工作?或者,也許檢查標誌成爲真的循環是錯誤的。

如果有人能啓發我,我的腳本錯在哪裏,我會非常感激。

回答

1

GM_setValue和GM_getValue確實在同一腳本內運行交叉表或窗口,但它們不運行交叉腳本。因此,當在腳本中設置該值時,它不會更改其他腳本的值。

嘗試本地存儲或存儲在外部數據庫中。

+0

根據域,postMessage也可以工作。 https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage –

相關問題