I am using Method 1 described here
Insert code into the page context using a content script
which is based on
Google Chrome "Application Shortcut": How to auto-load JavaScript?
This was working fine but after my browser (Chrome) updated to v71 this does not work anymore as my elements can't find the functions in my injected JS file anymore. I prepared an easy example:
manifest.json
{
"manifest_version": 2,
"name": "Test",
"version": "1.0.0",
"web_accessible_resources":
[
"Callbacks.js"
],
"content_scripts": [
{
"matches": ["https://stackoverflow.com/"],
"js": ["inject.js"],
"run_at": "document_idle"
}
],
"permissions": [
"activeTab"
]
}
inject.js
function newButton(id, cb)
{
var b = document.createElement("div");
b.setAttribute("onclick",cb);
b.style.margin = "50px"
b.style.width = "100px";
b.style.height = "100px";
b.style.background = "gray";
b.style.zIndex = 10000000;
b.style.position = "absolute";
b.id = id;
return b;
}
var cc= document.createElement("script");
cc.src = chrome.runtime.getURL('Callbacks.js') ;
document.body.appendChild(cc);
document.body.appendChild(newButton("myId", "myfun();"));
Callbacks.js
function myfun(){
window.alert("hallo");
}
When going on stackoverflow.com and clicking on the gray div I see this in my console
(index):1 Uncaught ReferenceError: myfun is not defined at HTMLDivElement.onclick ((index):1) onclick @ (index):1
However, I can still call myfun from the console directly.
Any ideas about this? With older Chrome version this still works.