After extensive searching I discovered Rob's answer: https://stackoverflow.com/a/11614440/7907844 that "webstore cannot be scripted by extensions".

I only want to extract the URL and pass it to the extension. Is there no way to achieve this? Could I query all opened tabs from background script, and keep the active tab URL in a variable accessible to extension?

1 Answers

0
miyagisan On

I ended up implementing background script that saves currently active tab URL in a variable that I access from within a popup.js because webstore doesn't allow us to run content scripts there. chrome.extension.getBackgroundPage() inside popup.js only worked when I used var currentUrl.

var currentUrl;
var activeTabId;

chrome.tabs.onUpdated.addListener(
    function (tabId, changeInfo, tab) {
        if (changeInfo.status === "complete") {
            chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
                let activeTab = tabs[0];
                currentUrl = activeTab.url;
                activeTabId = activeTab.id;
                if(currentUrl.includes("chrome.google.com/webstore/detail")) {
                    chrome.pageAction.show(activeTab.id);
                } else {
                    chrome.pageAction.hide(activeTab.id);
                }
                console.log("page loaded: ", currentUrl);
            });
        }
    });

chrome.tabs.onActivated.addListener(function (object) {
        activeTabId = object.tabId;
        chrome.tabs.get(activeTabId, function(tab){
            currentUrl = tab.url;
            if(tab.url.includes("chrome.google.com/webstore/detail")) {
                chrome.pageAction.show(object.tabId);
            } else {
                chrome.pageAction.hide(object.tabId);
            }
            console.log("loaded tab selected: ", tab.url);
        })
});