简体   繁体   English

我将如何处理? (Chrome扩展程序)

[英]How would I go about this? (Chrome extension)

I'm new to creating extensions, and I'm trying to become more comfortable with them. 我是创建扩展的新手,我正努力使它们变得更舒适。 Basically, I'm trying to create a simple extension that does the following: 基本上,我正在尝试创建一个执行以下操作的简单扩展:

  1. Open a new window (achieved). 打开一个新窗口(已实现)。
  2. Have the ability to 'click' a button from the extension (not achieved). 能够“点击”扩展中的按钮(未实现)。

I'm not sure how to approach having the extension click the button in a new window. 我不确定如何使用扩展名单击新窗口中的按钮。 How can I approach this? 我该如何处理? This is what I have right now: 这就是我现在所拥有的:

popup.html

<html>
        <body>
                <input type="button" id="the_button" value="My button"></input>
        </body>
        <script src="popup.js"></script>
</html>

popup.js

document.getElementById("the_button").addEventListener("click", function() {
    var win = window.open("http://www.roblox.com", "roblox", 400, 400)
    //Now what can I do to externally click buttons?
})

manifest.json

{
  "manifest_version": 2,
  "name": "Test",
  "description": "Test Extension",
  "version": "1.0",

  "icons": { 
    "48": "icon.png"
   },

  "permissions": [
    "http://*/*", 
    "https://*/*"
  ],

  "content_scripts": [{
    "matches": ["http://*/*", "http://*/*"],
    "js": ["jquery.js", "popup.js"]
  }],

  "browser_action": {
    "default_title": "This is a test",
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

First of all, you're making a mistake using the </input> closing tag: <input> tags don't need to be closed! 首先,使用</input>结束标记犯了一个错误: <input>标记不需要关闭! So change your popup.html into this: 因此,将您的popup.html更改为:

<html>
    <body>
        <input type="button" id="the_button" value="My button">
    </body>
    <script src="popup.js"></script>
</html>

Now, getting to the real question: 现在,进入真正的问题:

You need to create a new tab, then inject a content script into the page. 您需要创建一个新选项卡,然后将内容脚本插入页面。 Here is a quick solution: 这是一个快速的解决方案:

  1. Add the tabs permission to your manifest.json : tabs权限添加到manifest.json

     ... "permissions": [ "*://*/*", // this will match both http and https :) "tabs" ], ... 

    Remove the popup.js content script from the manifest , that's useless! 从清单中删除popup.js内容脚本 ,这是没有用的! You don't need it. 不用了

     ... "content_scripts": [{ // remove! "matches": ["http://*/*", "http://*/*"], // remove! "js": ["jquery.js", "popup.js"] // remove! }], // remove! ... 

    WARNING : When I say remove I mean trurly remove that lines from your manifest.json , do not use comments ( // ) and do not copy and paste my code over your code, just delete that four lines. 警告 :当我说删除时 ,是指从您的manifest.json彻底删除该行,不要使用注释( // ),也不要在代码上复制并粘贴我的代码,只需删除那四行。

  2. Now, in your popup.js you can inject a content script inside your page when you open the tab, like this: 现在,在您的popup.js您可以在打开标签页时在页面内注入内容脚本,如下所示:

     document.getElementById("the_button").addEventListener("click", function() { chrome.tabs.open({url:"http://www.roblox.com", active:"true"}, function(tab) { chrome.tabs.executeScript(tab.id, {file:"click_the_button.js", run_at: "document_end"}); // here is where you inject the content script ^^ } }); 
  3. This will create a new tab and inject inside it the script click_the_button.js , the one you will use to click the button inside the page (when it's loaded), whose code would be: 这将创建一个新选项卡,并将脚本click_the_button.js注入其中,您将使用该脚本单击页面内的按钮(加载时),其代码为:

     var thing = $("a#roblox-confirm-btn"); thing.click(); 

NOTE: if you want to use jQuery in your click_the_button.js script, you can as well inject it in the tab before it: 注意:如果要在click_the_button.js脚本中使用jQuery,也可以在其选项卡中插入jQuery:

document.getElementById("the_button").addEventListener("click", function() {
    chrome.tabs.open({url:"http://www.roblox.com", active:"true"}, function(tab) {
        chrome.tabs.executeScript(tab.id, {file:"jQuery.js", run_at: "document_start"});
        chrome.tabs.executeScript(tab.id, {file:"click_the_button.js", run_at: "document_end"});
    }
});

Resources you may find useful: 您可能会发现有用的资源:

You need tabs permission. 您需要标签权限。 I had the same problem before. 我之前也遇到过同样的问题。 The answer is here: 答案在这里:

Chrome extension; Chrome扩展程序; open a link from popup.html in a new tab 在新标签页中打开来自popup.html的链接

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

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