I have the code taken from Inject a script tag with remote src and wait for it to execute:
function injectScript(src){
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
console.log(script.src)
script.setAttribute('type', 'text/javascript');
script.addEventListener('load', resolve);
script.addEventListener('error', e => reject(e.error));
document.head.appendChild(script);
});
}
By writing
injectScript((chrome.runtime.getURL("/utils.js")))
.then(() => {
console.log("script loaded")
}).catch(error => {
console.log(error);
})
I can successfully inject my code from utils.js (below) into an html page which I have verified works with seperately injected html code with a button with onclick="foo()"
function foo(){
console.log("bar");
}
Now, my question is how do I modify my injectScript function to inject my brython code (below)? I have tried setting the type attribute to text/python
but otherwise keeping it the same, but this does not work. Similarly changing script.src = src
to script.textContent = src
does not work. In all cases, simply nothing happens without an error.
***python.js***
from browser import document, alert
def hello():
alert("hello world")
document['mybutton'].bind('click', hello)
Edit: Full code used for injecting brython code (console logs 'min script loaded' and 'stdlib script loaded' but not 'python loaded' nor alerts 'hello world':
injectScript("https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js")
.then(() => {
console.log('min script loaded!');
injectScript("https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.5/brython_stdlib.min.js")
.then(() => {
console.log('stdlib script loaded!');
injectPython((chrome.runtime.getURL("/python.js")))
.then(() => {
console.log("python loaded")
}).catch(error => {
console.log(error)
})
}).catch(error => {
console.error(error);
});
}).catch(error => {
console.error(error);
});