Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过Chrome扩展单击页面上的元素_Javascript_Google Chrome_Google Chrome Extension_Click - Fatal编程技术网

Javascript 通过Chrome扩展单击页面上的元素

Javascript 通过Chrome扩展单击页面上的元素,javascript,google-chrome,google-chrome-extension,click,Javascript,Google Chrome,Google Chrome Extension,Click,我正在尝试创建一个chrome扩展,当你点击扩展上的按钮时,它会点击网页上的一个元素,但由于某种原因,无论我怎么做,它都不会起作用 到目前为止我已经知道了 manifest.json { "manifest_version": 2, "name": "Such Activity", "description": "Wow", "version": "1.0", "permissions": [ "tabs", "<all_u

我正在尝试创建一个chrome扩展,当你点击扩展上的按钮时,它会点击网页上的一个元素,但由于某种原因,无论我怎么做,它都不会起作用

到目前为止我已经知道了

manifest.json

{  
  "manifest_version": 2,  

  "name": "Such Activity",  
  "description": "Wow",  
  "version": "1.0",    
  "permissions": [  
        "tabs", "<all_urls>"  
    ],  

  "browser_action": {  
    "default_icon": "icon.png",  
    "default_popup": "popup.html"  
  },  
     "content_scripts": [  
        {  
            "matches": [ "<all_urls>" ],  
           "js": ["content_script.js"]  
            }  
        ]  
}
我似乎得到的只是这个错误

Uncaught TypeError: Cannot read property 'click' of undefined
我已经摆弄这个好几个小时了,但我不能让它工作。感谢您的任何帮助

发行 看起来您误解了内容脚本的行为

引用官方文件:

内容脚本是在网页上下文中运行的JavaScript文件。通过使用标准文档对象模型(DOM),他们可以读取浏览器访问的网页的详细信息,或者对其进行更改

因此,在
popup.html
中使用
content\u script.js
没有多大意义,显然会导致错误,因为在
popup.html
页面中没有任何带有
class=“planet name”
的按钮

解决方案 要想做你想做的事情,你需要为你的
popup.html
创建一个不同的脚本,并在该按钮上添加一个监听器,这样当它被点击时,你可以将
content\u script.js
脚本注入页面,点击“planet”元素

因此,您必须:

  • 编辑您的
    manifest.json
    删除
    “content\u scripts”
    字段
  • 创建一个
    popup.js
    文件以添加到
    popup.html
    页面中
  • 添加按钮的侦听器单击
    popup.js
    内部,使用
    chrome.tabs.executeScript()
  • 编辑您的
    内容\u script.js
    ,使其直接单击页面上的按钮

  • 首先,编辑您的
    manifest.json
    ,它应该如下所示:

    {
    “清单版本”:2,
    “名称”:“此类活动”,
    “描述”:“哇”,
    “版本”:“1.0”,
    “权限”:[“选项卡”,”

    文档 我强烈建议您查看我的示例中使用的方法的文档,以充分了解它们的行为和属性,以下是一些您可能会发现有用的链接:

      • 方法
      • 方法

    Wow,非常感谢。我理解您现在需要完全注入脚本的方式,而以前它只是…模糊的。但是,它仍然不起作用。我不再收到错误,但元素没有被单击。然后添加类似
    alert(“内容脚本:我正在运行!”)的警报
    在你的
    content\u script.js
    中,这样你就可以看到它是否被正确注入…没有警报。作为参考,这是我试图将此应用到的网站:需要点击的行星位于right@RicoClark我编辑了这个问题,并在你提供的页面上自己测试了它。现在它工作了,有点小问题因为在已经加载的页面中使用
    run\u at:“document\u end”
    实际上不会运行任何内容。请用我的答案的新代码重新加载扩展名,看看它是否有效。@RicoClark我还添加了一个下载链接来下载示例(如果需要)。
    function ClickPlanet()
        {
            var planets = document.getElementsByClassName("planet-name");
            var randomplanet = Math.floor(Math.random() * planets.length);
            var clickplanet = planets[randomplanet];
            clickplanet.click();
            setInterval(function () { ClickPlanet() }, 2000);
        }
    
    document.addEventListener('DOMContentLoaded', function () {
        document.getElementById('clickactivity').addEventListener('click', ClickPlanet);
    });
    
    Uncaught TypeError: Cannot read property 'click' of undefined
    
    function injectTheScript() {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            // query the active tab, which will be only one tab
            //and inject the script in it
            chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});
        });
    }
    
    document.getElementById('clickactivity').addEventListener('click', injectTheScript);
    
    function clickPlanet() {
        var planets = document.getElementsByClassName("planet-name"),
            randomplanet = Math.floor(Math.random() * planets.length),
            clickplanet = planets[randomplanet];
    
        clickplanet.click();
    }
    
    clickPlanet();