jhen0409 / react-chrome-extension-boilerplate

Boilerplate for Chrome Extension React.js project
MIT License
2.14k stars 388 forks source link

Handle inject page on extension icon click #56

Open teckays opened 7 years ago

teckays commented 7 years ago

Is there a way to trigger Dock opening on extension icon click event?

jhen0409 commented 7 years ago

You can listen browser action and execute script in the tab via background page. (inject script in background page)

yiziz commented 6 years ago

@jhen0409 I am trying to do this also. Can you please provide an example?

I tried this, but it is not working:

    chrome.browserAction.onClicked.addListener((tab) => {
        chrome.tabs.executeScript(tab.id, { code: fetchRes, runAt: 'document_end' }, cb);
    });
cosst commented 6 years ago

@teckays @yiziz Did either of you ever get this to work properly? Same problem here and can't figure it out.

With your implementation @yiziz I do indeed see the load inject bundle success! message in the console upon clicking the extension icon, but the dock does not appear nor does it seem to actually affect the page at all.

cosst commented 6 years ago

@jhen0409 Perhaps you'd be willing to elaborate? What @yiziz posted doesn't seem to work. Thanks!

ianoti commented 5 years ago

@jhen0409 did you manage to resolve this? I tried it and I'm getting an error of chrome.runtime.lastError of {message: "Could not establish connection. Receiving end does not exist."}

palashkaria commented 4 years ago

I use this method content.js

background.js

chrome.browserAction.onClicked.addListener(function (tab) {
  // Send a message to the active tab
  chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    var activeTab = tabs[0];
    chrome.tabs.sendMessage(activeTab.id, {
      message: 'clicked_browser_action',
    });
  });
});
chrome.runtime.onMessage.addListener(function (request) {
  if (request.message === 'clicked_browser_action') {
    // check if already injected, otherwise
    document.createElement()
    reactDOM.render()
  }
});