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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With