I'm trying to inject a script into a site and from everything I've tried nothing works correctly.
When I try to inject a script I get this error:
Error handling response: TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
at chrome-extension://mjpoonklfkmkfofjemeehpkgljpkejef/popup.js:11:53
Here are my files:
popup.js
function getScript(file) {
var script = document.createElement('script');
script.src = chrome.runtime.getURL(file);
document.body.appendChild(script);
}
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let url = tabs[0].url;
if (url.includes('discord.com')) {
var s = getScript('inject.js');
(document.head || document.documentElement).appendChild(s);
} else {
window.close();
}
});
inject.js
console.log('inject.js loaded')
These also don't work on my end:
(document.body || document.documentElement).appendChild(s);
chrome.tabs.executeScript({
code: 'console.log("Injected Code!");'
});
chrome.extension.getBackgroundPage().appendChild(s);
document.getElementsByTagName('body')[0].appendChild(s);
document.body.appendClild(s)
document.head.appendChild(s);
I'm trying to inject the script to send data to the extension.