简体   繁体   English

单击按钮的Chrome扩展程序

[英]Chrome extension that clicks a button

I have a page with a button and I'm creating a chrome extension to click on it but it doesn't work: 我有一个带有按钮的页面,我正在创建一个Chrome扩展程序来单击它,但它不起作用:

<input type="submit" name="commit" value="add to basket" class="button"/>

This is my manifest.json 这是我的manifest.json

{
    "description": "Click",
    "manifest_version": 2,
    "name": "click-product-addtocart-button",
    "version": "0.1",

    "permissions": [
        "activeTab"
    ],

    "background": {
        "scripts": [
            "background.js"
        ]
    },

    "browser_action": {
        "default_icon": {
            "32": "/images/icons/16.png"
        },
        "default_title": "Click product"
    }
}

and this is my background.js 这是我的background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {
        code: "document.getElementByName('commit')[0].click();"
    });
});

I click on the app but nothing happens. 我单击该应用程序,但没有任何反应。 I have tried several things but that seems not to work 我已经尝试了几件事,但这似乎不起作用

Your manifest must request permission to access the current page by URL as well as just the current tab. 您的清单必须请求通过URL以及当前选项卡访问当前页面的权限。 Update your permissions to the following: 将您的权限更新为以下内容:

"permissions": [
     "activeTab",
     "*://*/*"
],

This will allow the extension to access any page. 这将允许扩展访问任何页面。 I would also recommend changing name to id . 我还建议将name更改为id This will prevent any confusion in the future. 这将防止将来出现任何混乱。 You would do this as follows: 您可以按照以下步骤进行操作:

<input type="submit" id="commit" value="add to basket" class="button">

Then for background.js: 然后对于background.js:

chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.executeScript({
        code: "document.getElementById('commit').click();"
    });
});

You'll notice I also removed the specificity of the tab that you had in your question. 您会注意到,我也删除了您在问题中使用的标签的特殊性。 This is not necessary. 这不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM