2016-10-28 48 views
-3

嗨,大家好,我想創建一個鉻擴展,一旦我點擊鉻擴展,腳本將啓動,並將循環檢查每1毫秒的按鈕與id「product-addtocart-button」。所以,一旦循環找到按鈕,它需要立即點擊。[]循環檢查按鈕鉻擴展不工作

我猜如果有必要,網站是Adidas.ae,一旦有新鞋出現,就會有鞋倒數倒計時,一旦倒計時結束,按鈕添加到卡將可用點擊,我需要點擊它。

-

我參加了@ Barmar的建議,改變了ID「產品addtocard鍵」的變量和函數參數使用它。在「tab.id」之後,我也從函數中拿走了大括號。

我的原代碼:

chrome.browserAction.onClicked.addListener(function(tab) { 
    chrome.tabs.executeScript(tab.id,{ 
function waitForElementToDisplay(#product-addtocart-button, 10) { 
     if(document.querySelector(#product-addtocart-button)!=null) { 
      alert("document.getElementById('product-addtocart-button').click()") 
      return; 
     } 
     else { 
      setTimeout(function() { 
       waitForElementToDisplay(#product-addtocart-button, 10); 
      }, 10); 
     } 
    } 

     }); 
}); 

更新的代碼:

var button = document.getElementById("product-addtocart-button"); 
var time = 10; 

chrome.browserAction.onClicked.addListener(function(tab) 
    { 
    chrome.tabs.executeScript(tab.id, 

     function waitForElementToDisplay(button, time) { 
       if(document.querySelector(button)!=null) 
       { 
        code: "document.getElementById(button).click();" 
        return; 
       } 
       else 
       { 
        setTimeout(function() { 
         waitForElementToDisplay(button, time); 
        }, time); 
       } 
      } 
     ); 
    } 
); 

注:這是一個擴展到這樣一個問題:Why is my Javascript Chrome extension code not working? (Loop check for button)。我已經接受了這些建議並需要更多幫助,因此我正在發佈我的更新代碼。

擴展的問題是當它被點擊時什麼都沒有發生。任何幫助?

+1

你有語法錯誤。仔細查看應該如何定義回調。問題可以作爲錯字被封閉。 –

+0

如果沒有來自控制檯的錯誤消息(特別是與語法相關的錯誤消息),我們無法對「沒有任何事情發生」做任何事情。 – ssube

+0

@ssube我在這裏提供了錯誤消息:http://stackoverflow.com/questions/40303653/chrome-extension-loop-check-for-button-errors –

回答

-1

specifications,你必須調用executeScript,如:

chrome.tabs.executeScript(tab.id,{code:"yourCodePackedIntoOneString"});

chrome.tabs.executeScript(tab.id,{file:"yourCodeFile.js"});

,但你在呼喚:

chrome.tabs.executeScript(tab.id,{function()etc...});

試試這個:

有一個文件名爲myWaitingLoop.js

function waitForElementToDisplay(){ 
    var button = document.querySelector("#product-addtocart-button"); 
    if (button){ 
     button.click(); 
    } else { 
     setTimeout(waitForElementToDisplay,100); 
    } 
} 
waitForElementToDisplay(); 

,然後在你的background.js腳本:

chrome.browserAction.onClicked.addListener(function(tab){ 
    chrome.tabs.executeScript(tab.id,{file:"myWaitingLoop.js"}); 
}); 
+0

你是個傳奇人物!正是我想要的。我一直在張貼,因爲我害怕沒有人會看到這個問題,根據我在不同的時間區域(阿拉伯聯合酋長國)和美國的時差在任何人都看不到。非常感謝你從迪拜:) –

+0

還有人downvoted它...... :( –