How would I go about this? (Chrome extension)

David

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"
  }
}
Marco Bonelli

First of all, you're making a mistake using the </input> closing tag: <input> tags don't need to be closed! So change your popup.html into this:

<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:

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

    Remove the popup.js content script from the manifest, that's useless! 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.

  2. Now, in your popup.js you can inject a content script inside your page when you open the tab, like this:

    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:

    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:

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:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How would I go about matching like this

From Dev

How would I go about combining these tables?

From Dev

How would I go about fixing this?

From Dev

Python - How would I go about doing this?

From Dev

How would I restore data of a Chrome extension only using backup?

From Dev

How would I go about masking specific $_GET variables as subfolders?

From Dev

how would i go about creating a Command Line Interface in javascript?

From Dev

How would I go about loading a JSON file using Laravel?

From Dev

How would I go about looping back to the beginning?

From Dev

How would I go about populating an SQL database with world languages?

From Dev

How would I go about putting StringVar()s within StringVar()?

From Dev

How would I go about iterating over this JSON data

From Dev

How would I go about "monitoring" the center of the screen?

From Dev

How would I go about searching for Users querying multiple associations?

From Dev

How would I go about randomly generating strings with certain parameters?

From Dev

How would I go about making a RewriteRule as described?

From Dev

How would I go about populating an SQL database with world languages?

From Dev

How would I go about interpreting this python code?

From Dev

How would I go about searching for Users querying multiple associations?

From Dev

How would I go about Game Networking with C#?

From Dev

How would I go about combining these two programs?

From Dev

How would I go about make function for object JavaScript?

From Dev

How would I go about hiding text under a moving picture

From Dev

How would I go about removing a subview when there is text in a UITextField

From Dev

How would I go about while looping this code?

From Dev

Jasmine: How would I go about mocking an edited field in a model?

From Dev

how would i go about adding some paragraphs on the go and show the result to the document with paragraph tags

From Dev

How would I go about creating another int() function in Python so I can understand it?

From Dev

Using a chrome extension, how would I read through all the HTML in a page and replace a certain part?

Related Related

  1. 1

    How would I go about matching like this

  2. 2

    How would I go about combining these tables?

  3. 3

    How would I go about fixing this?

  4. 4

    Python - How would I go about doing this?

  5. 5

    How would I restore data of a Chrome extension only using backup?

  6. 6

    How would I go about masking specific $_GET variables as subfolders?

  7. 7

    how would i go about creating a Command Line Interface in javascript?

  8. 8

    How would I go about loading a JSON file using Laravel?

  9. 9

    How would I go about looping back to the beginning?

  10. 10

    How would I go about populating an SQL database with world languages?

  11. 11

    How would I go about putting StringVar()s within StringVar()?

  12. 12

    How would I go about iterating over this JSON data

  13. 13

    How would I go about "monitoring" the center of the screen?

  14. 14

    How would I go about searching for Users querying multiple associations?

  15. 15

    How would I go about randomly generating strings with certain parameters?

  16. 16

    How would I go about making a RewriteRule as described?

  17. 17

    How would I go about populating an SQL database with world languages?

  18. 18

    How would I go about interpreting this python code?

  19. 19

    How would I go about searching for Users querying multiple associations?

  20. 20

    How would I go about Game Networking with C#?

  21. 21

    How would I go about combining these two programs?

  22. 22

    How would I go about make function for object JavaScript?

  23. 23

    How would I go about hiding text under a moving picture

  24. 24

    How would I go about removing a subview when there is text in a UITextField

  25. 25

    How would I go about while looping this code?

  26. 26

    Jasmine: How would I go about mocking an edited field in a model?

  27. 27

    how would i go about adding some paragraphs on the go and show the result to the document with paragraph tags

  28. 28

    How would I go about creating another int() function in Python so I can understand it?

  29. 29

    Using a chrome extension, how would I read through all the HTML in a page and replace a certain part?

HotTag

Archive