Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
alert('hello ' + document.location.href);
{
"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"
}
}
<!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>
// var app = chrome.runtime.getBackgroundPage();
function hello() {
chrome.tabs.executeScript({
file: 'alert.js'
});
}
document.getElementById('clickme').addEventListener('click', hello);
@apysais

This comment has been minimized.

Copy link

@apysais apysais commented Mar 15, 2017

tnx

@trevren11

This comment has been minimized.

Copy link

@trevren11 trevren11 commented May 28, 2017

👍

@shape-of-myHeart

This comment has been minimized.

Copy link

@shape-of-myHeart shape-of-myHeart commented Jul 1, 2017

thx

@gilgri

This comment has been minimized.

Copy link

@gilgri gilgri commented Sep 24, 2017

why background.js is empty ?
do you have an example that can read from the html and get show the values in popup.html?

@XuYanci

This comment has been minimized.

Copy link

@XuYanci XuYanci commented May 22, 2018

thanks a lot

@greatghoul

This comment has been minimized.

Copy link
Owner Author

@greatghoul greatghoul commented Jun 11, 2018

removed the background file.

@Gnanavelc7

This comment has been minimized.

Copy link

@Gnanavelc7 Gnanavelc7 commented Jun 26, 2018

how to redirect to .cshtml in default popup
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"--->here
}

@ebanner

This comment has been minimized.

Copy link

@ebanner ebanner commented Jul 31, 2018

thanks!

@puneettrivedi008

This comment has been minimized.

Copy link

@puneettrivedi008 puneettrivedi008 commented Aug 2, 2018

How can we execute the the complete javascript having multiple functions using chrome.tab.executescript? I am complete new to all this.

@Yagniksojitra

This comment has been minimized.

Copy link

@Yagniksojitra Yagniksojitra commented Aug 11, 2018

how to add a radio button and turn on and off the script with radio button ?

@fxnoob

This comment has been minimized.

@filip505

This comment has been minimized.

Copy link

@filip505 filip505 commented Dec 1, 2018

thanks 👍

@DanielOberlechner

This comment has been minimized.

Copy link

@DanielOberlechner DanielOberlechner commented Jan 15, 2019

I have followning error:
"Uncaught TypeError: Cannot read property 'addEventListener' of null"

what the heck am I doing wrong???
Greets

@rrpathi

This comment has been minimized.

Copy link

@rrpathi rrpathi commented Jan 18, 2019

thanks lot you save my life

@ole0

This comment has been minimized.

Copy link

@ole0 ole0 commented Mar 12, 2019

thank you!

@brenoprata10

This comment has been minimized.

Copy link

@brenoprata10 brenoprata10 commented Apr 2, 2019

Thanks 👍

@kartickmanna

This comment has been minimized.

Copy link

@kartickmanna kartickmanna commented Aug 2, 2019

Excellent...... and for Firefox browser.tabs.executeScript

@pupumeme

This comment has been minimized.

Copy link

@pupumeme pupumeme commented Nov 23, 2019

thank you

@sahin52

This comment has been minimized.

Copy link

@sahin52 sahin52 commented Jan 24, 2020

You are perfecto, spacibo, gracias

@vbharuka

This comment has been minimized.

Copy link

@vbharuka vbharuka commented Aug 8, 2020

Thanks a lot.

@deCodeIt

This comment has been minimized.

Copy link

@deCodeIt deCodeIt commented Aug 8, 2020

I have followning error:
"Uncaught TypeError: Cannot read property 'addEventListener' of null"
what the heck am I doing wrong???
Greets

did 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);
@deCodeIt

This comment has been minimized.

Copy link

@deCodeIt deCodeIt commented Aug 8, 2020

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);
}
@joelthorner

This comment has been minimized.

Copy link

@joelthorner joelthorner commented Sep 25, 2020

Omfg tnx!

@manoj9121

This comment has been minimized.

Copy link

@manoj9121 manoj9121 commented Dec 10, 2020

How to show popup for desktopcapture on button click? Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment