为什么我的Javascript Chrome扩展程序代码无法使用? (循环检查按钮) [英] Why is my Javascript Chrome extension code not working? (Loop check for button)

查看:146
本文介绍了为什么我的Javascript Chrome扩展程序代码无法使用? (循环检查按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着做一个chrome扩展,不断检查ID为product-addtocart-button的按钮,一旦找到它就会点击。



通过在线学习和研究,我已经与这个javascript一起走过了。我对JavaScript没有太多了解,所以我不知道发生了什么问题。



我的旧JavaScript文件非常光秃秃,我已经设置了它当我点击扩展按钮时,按钮会自动点击。



代码:

  chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(tab.id,{
code:document.getElementById('product-addtocart-button ').click();
});
});

现在,从研究中,我已经(试图)添加一个函数,在点击扩展按钮之后脚本将循环检查实际的扩展名,然后一旦找到,点击它。



background.js:

 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);
}
}

});
});

当我点击铬扩展按钮时,什么也没有发生。任何想法是怎么回事?

根据 specifications ,你必须调用executeScript: .executeScript(tab.id,{code:yourCodePackedIntoOneString});





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



但是您正在调用:

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



尝试一下,看看它是怎么回事。


I am trying to make a chrome extension that constantly checks for the button with the ID "product-addtocart-button", and as soon as it is found it will click.

I have came together with this javascript by learning and researching online. I don't know much about javascript, so I don't really know what is going wrong.

My old javascript file was very bare and I had it set up so when I clicked the extension button, the button would be automatically clicked.

Code:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id,{
        code: "document.getElementById('product-addtocart-button').click();"
    });
});

Now, from research, I have (attempted to) added a function where after clicking the extension button the script would loop check for the actual extension and then once found, click it.

background.js:

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);
        }
    }

        });
});

When I click on the chrome extension button nothing happens. Any idea what is going on?

解决方案

According to specifications, you have to invoke executeScript like:

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

or

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

but you are calling:

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

Try that and see how it goes.

这篇关于为什么我的Javascript Chrome扩展程序代码无法使用? (循环检查按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆