Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a FF equivalent to chrome.declarativeContent.onPageChanged?

I'm porting a Chrome extension to Firefox WebExtensions and I'm stuck on finding a workaround for chrome.declarativeContent.onPageChanged.

My FF Webextension contains a Page Action that must be shown when navigating on certain websites. However, none of the listeners in the available API seem to allow this.

In particular I've tried:

chrome.runtime.onInstalled.addListener(onChange);
chrome.tabs.onCreated.addListener(onChange);
chrome.tabs.onActivated.addListener(onChange);
chrome.tabs.onUpdated.addListener(onChange);
chrome.webNavigation.onDOMContentLoaded(onChange);
chrome.webNavigation.onCreatedNavigationTarget(onChange);

Are there any known workarounds?

like image 726
ktouchie Avatar asked Aug 31 '16 14:08

ktouchie


1 Answers

You'll have to show the pageAction manually because declarativeContent API is not yet supported.

chrome.pageAction.show(tabId);
chrome.pageAction.hide(tabId);

In case the rules are based on URL matching the implementation is quite easy:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo.status === 'complete' && tab.url.match(/something/)) {
        chrome.pageAction.show(tabId);
    } else {
        chrome.pageAction.hide(tabId);
    }
});

However in case it's DOM element-based you'll have to use a content script:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo.status === 'complete' && tab.url.match(/something/)) {
        chrome.tabs.executeScript(tabId, {
            code: 'document.querySelector(' + JSON.stringify(someSelector) + ')'
        }, function(results) {
            if (results[0]) {
                chrome.pageAction.show(tabId);
            } else {
                chrome.pageAction.hide(tabId);
            }
        });
    } else {
        chrome.pageAction.hide(tabId);
    }
});

This is a very simplified code. It can be enhanced to look for elements while a page is loading.

like image 108
wOxxOm Avatar answered Oct 21 '22 08:10

wOxxOm