I recently discovered the chrome extension development and got stuck with the runtime.excuteScript method, the callback in 3rd argument systematically returns me an empty object ...
For brevity, I will spare you all of my manifest.json(v2):
▼ manifest permissions:
"permissions": [
"storage",
"cookies",
"tabs",
"background",
"activeTab",
"<all_urls>",
"*://*/*"
]
▼ manifest content-script:
"content_scripts": [
{
"matches": ["<all_urls>"],
"run_at": "document_end",
"js": ["js/content-script.js"]
}
]
My goal is to send the content of the localStorage to my extension.
▼ pop-up.js:
chrome.tabs.executeScript(
null,
{ file: "js/content-script.js" },
(result) => {
if(result) console.log( " Result of content-script:",result )
else console.log(" No content-script, no result")
}
);
▼ content-script.js:
localStorage;
▼ output in the console of extension:
Result of content-script: Array(1)
▶︎ 0: {}
length:1
▶︎ __proto__: Array(0)
Please make this a wonderful evening by explaining the mistake to me! Thank you!
for me, it turned out to be the tab to execute the script is not fully loaded, so that the script did not run at all. now, i have a small pulling logic to check the tab status and execute the script after its status becomes to be 'complete'. it now returns the boolean value with no problem.