I’m writing Chrome Extension where my extension script needs some data for current tab. I have content script which have access to DOM and window object. It is run with “world” “MAIN” property, non isolated, because I want access to actual window object with augmented with specific data with third party script. I have following code:
Manifest.json:
{
"manifest_version": 3,
"name": "Plugin",
"version": "1.0",
"description": "Plugin",
"permissions": [
"cookies",
"activeTab",
"storage",
"scripting",
"tabs"
]
"host_permissions": [
<list of urls>
],
"content_scripts": [
{
"world": "MAIN",
"js": ["content.js"],
"matches": [
<list of urls>
]
}
],
"action": {
"default_popup": "popup.html"
},
"icons": {
"16": "icon_16x16.png",
"48": "icon_48x48.png",
"128": "icon_128x128.png"
}
}
content.js
chrome.runtime.onConnect.addListener(port => {
if (port.name === 'test') {
port.postMessage({
type: 'test',
status: window.someCustomProperty,
})
}
});
And my script from popup.html
const port = chrome.tabs.connect(activeTabId, {name: 'test'});
port.onMessage.addListener((message) => {
resolve(message.status);
console.log('MESSAGE', message);
});
port.onDisconnect.addListener(() => {
reject();
console.log('DISCONNECTED');
});
Problem is that it isn’t working. When I run my content.js script with "world": "MAIN"
in manifest.json, chrome.runtime
is undefined. When I run content.js script without "world": "MAIN"
, window.someCustomProperty
is undefined, because it runs in isolation. I’ve read several resources about similiar problem on SO and web, but none solved this issue.