19

I am trying to execute javascript on a page when I click on a button in popup.html. I tried to use such a way:

In background.js:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo){
    if(changeInfo.status == "loading") {
        insert(tabId);
    }
});
function insert(tabId) {
    chrome.tabs.get(tabId, function(tab) {
        $('button').click(function() {
            chrome.tabs.executeScript(tab.id, {file: 'js/alert.js'});
        });
    });
}

Alert.js consists only of one string: alert('works');

Alert is just an example. Real script should do some DOM manipulation with opened tab after user clicks on a button im popup.html.

2
  • You seem to have messed up some concepts (such popup, background-page, etc). It would help to post your manifest as well. Commented Dec 24, 2013 at 19:57
  • 1
    The presented background.js doesn't meet your requirements, and seems useless. In your content script, place alert into a function, say 'handleButtonClick'. Make sure the content script is registered in the manifest. Then in the popup.html just invoke your function: chrome.tabs.executeScript({code: 'handleButtonClick()'});. Commented Dec 24, 2013 at 21:03

2 Answers 2

44

I wrote a demo for your need.

https://gist.github.com/greatghoul/8120275


alert.js

alert('hello ' + document.location.href);

background.js

// empty file, but needed

icon.png

icon

manifest.json

{
  "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"
  },

  "background": {
    "scripts": ["background.js"],
    "persistent": false
  }
}

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>

popup.js

// var app = chrome.runtime.getBackgroundPage();

function hello() {
  chrome.tabs.executeScript({
    file: 'alert.js'
  }); 
}

document.getElementById('clickme').addEventListener('click', hello);
Sign up to request clarification or add additional context in comments.

6 Comments

I upvoted for your efforts, but what for you added the empty background.js?
@VladGolubev there is no such api, but you can insert html use pure javascript document.createElement('tag_name'); or jQuery to go. it's easy.
While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. It would be trivial to just copy the contents of your Gist into this answer. However, you do not state the licensing terms for the code in your gist. Thus, it is not clear that it would be permissible for me to just do that.
Where will this script executed? On popup.html? Or the active webpage (like contentscript)?
For manifest V3: \ 1. you need to use chrome.scripting.executeScript({ & files stackoverflow.com/questions/66549560/… \ 1. for the target you may use chrome.tabs.query({currentWindow: true, active: true}, function(tabs){ stackoverflow.com/questions/7303452/…
|
5

You can also use Messaging:

in popup.js

document.getElementById("clicked-btn").addEventListener("click", function(e) {
    chrome.runtime.sendMessage({'myPopupIsOpen': true});
});

in background.js

chrome.runtime.onMessage.addListener(function(message, sender) {
      if(!message.myPopupIsOpen) return;

      // Do your stuff
});

Not tested but should works, further informations about Messaging.

2 Comments

This is a better way to do it rather than running raw scripts like in @greatghoul's answer
Theoractly It may be better, but it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.