user6468132 user6468132 - 1 year ago 77
jQuery Question

Chrome extension window onload jquery

How am I going to write something when user clicks extension icon and loads the window?

I've already tried

window.onload = function() {

and ]

console.log('document is ready');

but still there isn't any log?

Answer Source

Maybe it's not the best idea but you can use chrome.tabs.sendMessage and chrome.runtime.onMessage.addListener to communicate between contentscript.js and popup.js

contentscript.js is running directly in page scope so you can easily detect when page is ready. After that you can send a message chrome.tabs.sendMessage(, {action: 'pageReady'});. In popup.js you are listening to:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.action === 'pageReady') {
        // Do what you want to do on page ready

It should works good for you. You can also send response to the caller:

  1. contentscript.js send information to the popup.js that page is ready.
  2. popup.js do what you want to do (show table).
  3. popup.js send information to the contentscript.js that table is added.

EDIT I'm not sure about my solution because I found information that:

The popup, while being an extension page, is not a background page. It is only accessible when it is open

I don't know if it is possible to listen on events in popup. You need to check it.