0

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

I've already tried

window.onload = function() { console.log("das"); }

and ]

$(document).ready(function(){ console.log('document is ready'); });

but still there isn't any log?

user6468132
  • 383
  • 1
  • 5
  • 18
  • 1
    Possible duplicate of [Cannot get Chrome popup.js to use console.log](http://stackoverflow.com/questions/14858909/cannot-get-chrome-popup-js-to-use-console-log) – wOxxOm Aug 27 '16 at 11:00
  • No the console.log is just a debugger, to understand whether it gets there. – user6468132 Aug 27 '16 at 11:21
  • I don't understand your comment. The linked answer tells how to open the correct devtools console window for the popup. – wOxxOm Aug 27 '16 at 11:22
  • Ok let me explain. When the window is loaded, I want to show a table, but it doesn't. and there isn't any logs in the console. I couldn't trigger window.load in jquery. – user6468132 Aug 27 '16 at 11:24
  • This can be caused by a variety of reasons. One of the most common is using embedded ` – wOxxOm Aug 27 '16 at 11:27

1 Answers1

1

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(tabs.id, {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.

Zacol
  • 93
  • 5
  • It's not even clear what the OP wants and where and what caused the problem. If your guess is right you must be psychic. Otherwise you've wasted your time on a poorly presented question. – wOxxOm Aug 27 '16 at 13:00