Chrome Extension how to send data from content script to popup.html Chrome Extension how to send data from content script to popup.html javascript javascript

Chrome Extension how to send data from content script to popup.html


Although you are definitely in the right direction (and actually pretty close to the end), there are several (imo) bad practises in your code (e.g. injecting a whole library (jquery) for such a trivial task, declaring unnecessary permissions, making superflous calls to API methods etc).
I did not test your code myself, but from a quick overview I believe that correcting the following could result in a working solution (although not very close to optimal):

  1. In manifest.json: Change the order of the content scripts, putting jquery first. According to the relevant docs:

    "js" [...] The list of JavaScript files to be injected into matching pages. These are injected in the order they appear in this array.

    (emphasis mine)

  2. In contentscript.js: Move the chrome.runtime.sendMessage({...}) block inside the onMessage listener callback.


That said, here is my proposed approach:

Control flow:

  1. A content script is injected into each page matching some criteria.
  2. Once injected, the content scripts send a message to the event page (a.k.a. non-persistent background page) and the event page attaches a page-action to the tab.
  3. As soon as the page-action popup is loaded, it sends a message to the content script, asking for the info it needs.
  4. The content script processes the request, and responds so the page-action popup can display the info.

Directory structure:

          root-directory/           |_____img                 |_____icon19.png                 |_____icon38.png           |_____manifest.json           |_____background.js           |_____content.js           |_____popup.js           |_____popup.html

manifest.json:

{  "manifest_version": 2,  "name": "Test Extension",  "version": "0.0",  "offline_enabled": true,  "background": {    "persistent": false,    "scripts": ["background.js"]  },  "content_scripts": [{    "matches": ["*://*.stackoverflow.com/*"],    "js": ["content.js"],    "run_at": "document_idle",    "all_frames": false  }],  "page_action": {    "default_title": "Test Extension",    //"default_icon": {    //  "19": "img/icon19.png",    //  "38": "img/icon38.png"    //},    "default_popup": "popup.html"  }  // No special permissions required...  //"permissions": []}

background.js:

chrome.runtime.onMessage.addListener((msg, sender) => {  // First, validate the message's structure.  if ((msg.from === 'content') && (msg.subject === 'showPageAction')) {    // Enable the page-action for the requesting tab.    chrome.pageAction.show(sender.tab.id);  }});

content.js:

// Inform the background page that // this tab should have a page-action.chrome.runtime.sendMessage({  from: 'content',  subject: 'showPageAction',});// Listen for messages from the popup.chrome.runtime.onMessage.addListener((msg, sender, response) => {  // First, validate the message's structure.  if ((msg.from === 'popup') && (msg.subject === 'DOMInfo')) {    // Collect the necessary data.     // (For your specific requirements `document.querySelectorAll(...)`    //  should be equivalent to jquery's `$(...)`.)    var domInfo = {      total: document.querySelectorAll('*').length,      inputs: document.querySelectorAll('input').length,      buttons: document.querySelectorAll('button').length,    };    // Directly respond to the sender (popup),     // through the specified callback.    response(domInfo);  }});

popup.js:

// Update the relevant fields with the new data.const setDOMInfo = info => {  document.getElementById('total').textContent = info.total;  document.getElementById('inputs').textContent = info.inputs;  document.getElementById('buttons').textContent = info.buttons;};// Once the DOM is ready...window.addEventListener('DOMContentLoaded', () => {  // ...query for the active tab...  chrome.tabs.query({    active: true,    currentWindow: true  }, tabs => {    // ...and send a request for the DOM info...    chrome.tabs.sendMessage(        tabs[0].id,        {from: 'popup', subject: 'DOMInfo'},        // ...also specifying a callback to be called         //    from the receiving end (content script).        setDOMInfo);  });});

popup.html:

<!DOCTYPE html><html>  <head>    <script type="text/javascript" src="popup.js"></script>  </head>  <body>    <h3 style="font-weight:bold; text-align:center;">DOM Info</h3>    <table border="1" cellpadding="3" style="border-collapse:collapse;">      <tr>        <td nowrap>Total number of elements:</td>        <td align="right"><span id="total">N/A</span></td>      </tr>      <tr>        <td nowrap>Number of input elements:</td>        <td align="right"><span id="inputs">N/A</span></td>      </tr>      <tr>        <td nowrap>Number of button elements:</td>        <td align="right"><span id="buttons">N/A</span></td>      </tr>    </table>  </body></html>


You can use localStorage for that. You can store any data in a hash table format in browser memory and then access it any time. I'm not sure if we can access localStorage from the content script (it was blocked before), try to do it by yourself. Here's how to do it through you background page (I pass data from content script to background page first, then save it in localStorage):

in contentScript.js:

chrome.runtime.sendMessage({  total_elements: totalElements // or whatever you want to send});

in eventPage.js (your background page):

chrome.runtime.onMessage.addListener(    function(request, sender, sendResponse){       localStorage["total_elements"] = request.total_elements;    });

Then you can access that variable in popup.js with localStorage["total_elements"].

Maybe you can access localStorage directly from the content script in modern browsers. Then you don't need to pass the data through your background page.

Nice reading about localStorage: http://diveintohtml5.info/storage.html