Last active
February 25, 2025 15:42
-
-
Save greatghoul/8120275 to your computer and use it in GitHub Desktop.
Execute script after click in popup.html (chrome extension) http://stackoverflow.com/questions/20764517/execute-script-after-click-in-popup-html-chrome-extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alert('hello ' + document.location.href); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "Click to execute", | |
"description": "Execute script after click in popup.html (chrome extension) http://stackoverflow.com/questions/20764517/execute-script-after-click-in-popup-html-chrome-extension.", | |
"version": "1.0", | |
"icons": { | |
"48": "icon.png" | |
}, | |
"permissions": [ | |
"tabs", "<all_urls>" | |
], | |
"browser_action": { | |
"default_icon": "icon.png", | |
"default_popup": "popup.html" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body style="width: 300px"> | |
Open <a href="http://stackoverflow.com" target="_blank">this page</a> and then | |
<button id="clickme">click me</button> | |
<script type="text/javascript" src="popup.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// var app = chrome.runtime.getBackgroundPage(); | |
function hello() { | |
chrome.tabs.executeScript({ | |
file: 'alert.js' | |
}); | |
} | |
document.getElementById('clickme').addEventListener('click', hello); |
Thanks a lot.
I have followning error:
"Uncaught TypeError: Cannot read property 'addEventListener' of null"
what the heck am I doing wrong???
Greetsdid you figure this out? Currently having the same problem
My popup.js looks like the following:
// var app = chrome.runtime.getBackgroundPage();
function hello() {
chrome.tabs.executeScript({
file: 'alert.js'
});
}
var readyStateCheckIntervalLocal = setInterval(function() {
if (document.readyState === "complete") {
clearInterval(readyStateCheckIntervalLocal);
document.getElementById('clickme').addEventListener('click', hello);
}
}, 10);
On a side note, you don't really need the alert.js file as the following will work too.
function hello() {
alert('hello ' + document.location.href);
}
Omfg tnx!
How to show popup for desktopcapture on button click? Please help
Thanks for the work here. I upvoted on StackOverflow :)
Wondering if you could extend it?
I would like to send a variable to the alert.js
Say
popup.html has
<input id="var1" type="text">
<input id="var2" type="text">
<button id="clickme">click me</button>
Then what to do with alert.js or the popup.js to get the variables to alert?
popup.js
function hello() {
chrome.tabs.executeScript({
file: 'alert.js'
variable: var1, var2
});
}
document.getElementById('clickme').addEventListener('click', hello);
Or how please?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are perfecto, spacibo, gracias