Trigger/invoke a Chrome extension from a web page
- Make a "fake" ajax call in your web page code.
- Intercept it in your extension (see https://medium.com/@gilfink/adding-web-interception-abilities-to-your-chrome-extension-fb42366df425)
- In the intercept, cancel the "fake" call and implement your desired behaviour.
create a manifest with background.js and content.js . Use
chrome.tabs.sendMessage(tabId, {}, function() { ... });
in background to send messages to content script which is injected into every webpage that is opened when extension is installed and enabled . On the content.js script use
chrome.runtime.onMessage.addListener(function(req, sender, callback) {
// here use condition to find out when this extension's popup.html should be opened
// and call the callback function which was passed in the argument list initially
callback("something");
});
Here the callback function defines in background.js and passed to content.js is the code for opening a new extension window such as
var panel_props = {
type: 'panel',
'width': width,
'height': height,
'left': left,
'top': top,
url: "chrome-extension://" + <extensionid>+ "/index.html"
}
chrome.windows.create(panel_props ,function (newWindow) {
vid = newWindow.id;
});
You can inject your content-script
to every page (register it in extension manifest) and alter the page html to add your button
or a
with your custom id.
The execution environment example explains it pretty good how you will trigger an event from the page to your content script. After you manage the trigger you can do anything you want as the extension logic.
Also keep in mind that this will require your extension's content-script
to be injected to every page the user visits. It is not possible to actually trigger the execution of your content-script
from the page if thats what you were asking.