Page 1 of 1

how to execute javascript inside window from chrome?

Posted: 2021-11-20, 11:16
by BZZZZ
this works on sites without CSP:

Code: Select all

Services.obs.addObserver({
  "observe":windowWithXray=>{
    try{
      windowWithXray.eval("console.log(123)")
    }catch(err){
      console.error(err)
    }
  }
},"content-document-global-created",false)
but on sites with CSP like https://developer.mozilla.org/en-US/ it Error: call to eval() blocked by CSP

how to execute javascript inside window with CSP?

Re: how to execute javascript inside window from chrome?

Posted: 2021-11-20, 11:25
by Moonchild
It is working exactly as-intended.

Script injection when CSP is enforced is tricky (on purpose, because that is exactly what CSP is meant to control!). You'll have to see how other extensions get around this.

Re: how to execute javascript inside window from chrome?

Posted: 2021-11-21, 00:17
by moonbat
You can directly gain access to the DOM of a page displayed in a tab without trying to inject scripts. Take a look at Pure URL's code for how to do this. Specifically the pageModifier.init() function.

Re: how to execute javascript inside window from chrome?

Posted: 2021-11-24, 13:46
by BZZZZ
is this good way to inject javascript?

Code: Select all

{
  {
    const o={}
    Components.utils.import("resource://gre/modules/jsdebugger.jsm",o)
    o.addDebuggerToGlobal(globalThis)
  }
  const dbg=Debugger()
  Services.obs.addObserver({
    "observe":windowWithXray=>{
      try{
        const result=dbg.makeGlobalObjectReference(windowWithXray).executeInGlobal("console.log(1)")
        if(result.throw)console.error(result)
      }catch(err){
        console.error(err)
      }
    }
  },"content-document-global-created",false)
}

Re: how to execute javascript inside window from chrome?

Posted: 2021-11-24, 16:13
by Moonchild
OK this has nothing to do with browser development and you're clearly not listening to suggestions.
Locking thread.