How Would I Go About This? (Chrome Extension)
Solution 1:
NOTE: since January 2021, use Manifest V3 with chrome.scripting.executeScript()
and the scripting
permission and move "*://*/*"
to host_permissions
instead of using the deprecated chrome.tabs.executeScript()
with the tabs
permission.
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:
Add the
tabs
permission to yourmanifest.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.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 ^^ } });
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:
Solution 2:
You need tabs permission. I had the same problem before. The answer is here:
Post a Comment for "How Would I Go About This? (Chrome Extension)"