Clicking an element on a page through Chrome Extension

Deadly 提交于 2019-11-28 17:06:21

Issue

Looks like you are misunderstanding the content script's behavior.

Quoting the official documentation:

Content scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.

So, using your content_script.js in your popup.html doesn't make much sense, and obviously results in an error because you don't have any button with class="planet-name" in your popup.html page.

Solution

To do what you want, you need to create a different script for your popup.html, and add a listener to that button, so when it gets clicked, you can inject the content_script.js script into the page to click the "planet" element.

So you'll have to:

  1. Edit your manifest.json removing the "content_scripts" field
  2. Create a popup.js file to add in your popup.html page
  3. Add a listener for the button click inside popup.js to inject the content_script.js file using chrome.tabs.executeScript()
  4. Edit your content_script.js to make it click the button directly on the page

  1. First of all, edit your manifest.json, it should look like this:

    {  
        "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"  
        }
    }
    
  2. Then, remove your content_script.js from the popup.html and replace it with the popup.js script:

    <!doctype html>  
    <html>  
        <head><title>activity</title></head>  
    <body>  
        <button id="clickactivity">click</button>  
        <script src="popup.js"></script> 
    </body>
    </html>  
    
  3. Create the popup.js script and add the listener to the button that will inject the script into the current page:

    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);
    
  4. Now, in your content_script.js, you'll need to click the button directly, and also you will not need to use DOMContentReady, because Chrome waits to inject the script by itself. So, your new content_script.js will be:

    function clickPlanet() {
        var planets = document.getElementsByClassName("planet-name"),
            randomplanet = Math.floor(Math.random() * planets.length),
            clickplanet = planets[randomplanet];
    
        clickplanet.click();
    }
    
    clickPlanet();
    

Working example

Download a working example of the extension from HERE.

Documentation

I strongly suggest you to take a look at the documentation for the methods used in my example to fully understand their behavior and their properties, here are some links you may find useful:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!