I'm starting to learn how to develop a chrome extention (manifest 3) and I have some result I don't understand. My final goal is to get the cookie value when a specific website is open, to send a request to this web site (fetch) and to show the json received in a new tab (I developed a javascript to send the request and it's working but I now want to get the cookie automaticaly with a chrome extention).
To start my learning, first step is to open a new tab when I'm on the website. I tried the below code in my background.js but the new tab is created when I activate my extention even if the website is not open and no new tab created after even if my condition is ok. Then I think I missed something but I don't knwo what.
Could someone explain me why, where I'm wrong and if not the good way, which chrome API I should see ?
chrome.runtime.onInstalled.addListener(function () {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: "www.google.com", schemes: ["https"] },
}),
],
actions: [chrome.tabs.create({})],
},
]);
});
});
Your code actions: [chrome.tabs.create({})]
is wrong. The method chrome.tabs.create
will be called when the extension installed. Maybe you want to trigger your popup.html when clicking the extension. If so, you can use actions: [ new chrome.declarativeContent.ShowPageAction() ]
. Then use chrome.action.onClicked.addListener
, see here.
If you want to automatically open a new tab when some website is opened, you can use chrome.tabs.onCreated.addListener
, see here, handle the event, filter the url, then do sth.