0

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);
    });
w1nter
  • 337
  • 4
  • 23
  • 1
    Browser can't run brython directly so you need to inject the interpreter first. – wOxxOm Sep 09 '21 at 17:35
  • Do you mean injecting something other than brython.min.js and brython_stdlib.min.js (have injected both and call injectScript within their initial .then() clause, still no dice)? Talk to me as if i've never used brython before... – w1nter Sep 13 '21 at 17:30
  • have editted the fuller script, thanks – w1nter Sep 15 '21 at 09:49
  • I doubt they will run on a page with strict CSP. Try downloading these scripts (manually) into the extension directory and then inject them using chrome.runtime.getURL. – wOxxOm Sep 15 '21 at 12:57
  • Still no luck. I still get console logs for min/stdlib script loaded but not 'python loaded'. No CSP warnings (didn't have that before loading the brython src urls either). Logging the python.js script src during injectScript function says chrome-extension://mcclncbnkdfngjnmjjenobbadjoaabnp/python.js which seems reasonable, but still, nothing happens – w1nter Sep 16 '21 at 14:08

0 Answers0