0

As I understand, it's not possible to inject an open tab with an iFrame if it's a system page. Using this for reference I have my extension opening as an iFrame. However I would like the extension to open as a popup (what seems to be default behaviour for most extension) if the tab is not injectable.

I would presume it's a function that I could call in the background.js file, instead of sending a message to the content-script.js but looking for the Docs I can't find anything relevant.

chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    chrome.tabs.sendMessage(tabs[0].id,"toggle");
})});

SimilarWeb do a nice example of this. -Clicking on SimilarWeb when there is just a blank tab:

Clicking on SimilarWeb when there is just a blank tab

-Clicking on SimilarWeb with a website loaded in a tab:

enter image description here

This sort of works, but only once each time the browser is opened background.js -

chrome.browserAction.onClicked.addListener(function () {
  chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
    if (tabs[0].url.includes("chrome://")) {
      chrome.browserAction.setPopup({popup: "index.html"});
    } else {
      chrome.tabs.sendMessage(tabs[0].id, "toggle");
    }
  })
});

colouredFunk
  • 778
  • 1
  • 13
  • 35
  • Messaging would work if you have declared `"content_scripts"` with a script that has chrome.runtime.onMessage listener: you can check chrome.runtime.lastError in the callback of sendMessage. BTW no need for query(), just use the `tab` parameter of [onClicked](https://developer.chrome.com/extensions/browserAction#event-onClicked) callback. – wOxxOm Feb 08 '20 at 12:58
  • Thanks for your reply, but messaging is already working as I stated, it's knowing how to call the app as a Popup rather than an iFrame that is the issue? – colouredFunk Feb 09 '20 at 09:40
  • I guess you can remove "default_popup" from manifest.json and use chrome.browserAction.onClicked in the background script. Another listener chrome.tabs.onUpdated would send a message to the tab on URL changes and if there's no response it would call chrome.browserAction.setPopup on this tab id only. – wOxxOm Feb 09 '20 at 10:42
  • The problem with this approach, it takes two clicks for the popup to open? – colouredFunk Feb 13 '20 at 11:11
  • Eh? No it shouldn't because onUpdated runs before the icon is clicked. – wOxxOm Feb 13 '20 at 11:19
  • Perhaps I've miss-understood? I've added my code to a Plunker to help get how it's looking https://plnkr.co/edit/IcB3hMVisJCmr4k24LE0?p=info – colouredFunk Feb 13 '20 at 13:48
  • setPopup should be used inside onUpdated. – wOxxOm Feb 13 '20 at 13:52
  • But that means it will run everytime that tab is updated - https://plnkr.co/edit/GPWajFGMqDZznIqee6d1?p=info – colouredFunk Feb 13 '20 at 14:15
  • And why is that a problem? P.S. 1) query() is not needed and will be wrong when the tab is inactive, simply use `tab` parameter of onUpdated, 2) See the documentation for setPopup and use a tabId parameter. – wOxxOm Feb 13 '20 at 15:16
  • Im sorry I’m not following you. Could you update the plnkr to show what you mean? I’ve been stuck on this for days now and pretty sure I’ve gone grey because of it – colouredFunk Feb 13 '20 at 16:28

0 Answers0