简体   繁体   English

如何通过将侦听器添加到“窗口”来从Chrome扩展程序单击网页上的按钮?

[英]How to click a button on a webpage from Chrome extension by adding a listener to `window`?

I'm writing a simple Chrome extension which will click a button inside an internal website of my company. 我正在编写一个简单的Chrome扩展程序,它将在我公司内部网站内单击一个按钮。 Specifically, the website contains a button "Show more" which upon click will show more data. 具体而言,该网站包含一个“显示更多”按钮,单击该按钮将显示更多数据。

My problem is that when I execute the Javascript needed to click the button in Chrome console everything works. 我的问题是,当我执行单击Chrome控制台中的按钮所需的Javascript时,一切正常。 However when I execute it from the content.js script of the extension the button is not clicked. 但是,当我从扩展的content.js脚本执行它时,未单击按钮。

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

{
 "manifest_version": 2,
 "name": "asd",
 "description": "qwe",
 "version": "0.1",
 "author": "abc",
 "browser_action": {
   "default_icon": "asd.png",
   "default_title": "Have a good day",
   "default_title": "asd"
},
"permissions": ["activeTab", "https://ajax.googleapis.com/"],
"content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["content.js"],
      "run_at": "document_end",
      "all_frames": true
    }
  ]
}

the content.js is like this: content.js是这样的:

window.addEventListener('load', function(event) {
    console.log("page load!");
    console.log(window.location.href);
    console.log()
    var pageURL = window.location.href;
    if (pageURL.indexOf('example.com')) {
        console.log('example.com');
        var metaTags = document.querySelectorAll('meta');
        for (var i = 0; i < metaTags.length; i++) {
            console.log(metaTags[i].getAttribute('content'));

            if (metaTags[i].getAttribute('name') === 'application-name' && metaTags[i].getAttribute('content') === 'XYZ') {
                console.log('got match');
                var buttonClass = document.getElementsByClassName('source-viewer-more-code');
                console.log('buttonClass = ' +  JSON.stringify(buttonClass));
                var button = buttonClass[0];
                console.log("button = " + button);
            }
        }
    }
});

I do see all console.log prints, however JSON.stringify(buttonClass) prints an empty object and console.log("buttonw = " + button); 我确实看到了所有console.log打印,但是JSON.stringify(buttonClass)打印了一个空对象, console.log("buttonw = " + button); prints undefined. 打印未定义。

I'm currently running the extension locally. 我目前正在本地运行扩展程序。 Is this a Chrome permissions issue or something else? 这是Chrome权限问题还是其他原因?

If you can read meta tags,I guess it is not an issue with permissions, Make sure that Button is in DOM when you are trying to get it by "getElementsByClassName"(One possible reason for button not in DOM is some js in the page might be populating the button to DOM after an ajax request). 如果您可以读取元标记,我想这不是权限问题,当尝试通过“ getElementsByClassName”获取按钮时,请确保Button位于DOM中(按钮不在DOM中的一个可能原因是页面中的一些js ajax请求后,可能会将按钮填充到DOM)。 (When you execute in console, you might be executing after the element is populated in DOM ) if you expect a button and it is not in the dom use something like setTimeout and wait for the button to be populated by other js in the page. (当您在控制台中执行时,您可能会在DOM中填充元素之后执行),如果您希望有一个按钮但不在dom中,请使用setTimeout之类的内容,然后等待该按钮由页面中的其他js填充。

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

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