0

I'm trying to make a tampermonkey script available to other tampermonkey scripts via the @require functionality, which is fine, but I also want to expose functions in the script which is being required to the scripts consuming it. I've tried this:

Script to be @required:

window.functionName = () => { console.log('Called from @required script') }

But even with unsafeWindow enabled in the consumer script, I still can't seem to access that function via unsafeWindow.functionName(), nor window.functionName().

Obviously I'd like to avoid using unsafeWindow if possible, but at the moment I can't get it to work regardless.

My alternate solution would be to have the script being @required script listen for window events.. something like this:

Script using the @required script:

window.dispatchEvent(new CustomEvent('CallRequiredFunction', { 
  detail: { 
    functionName: 'functionName',
    functionArguments: []
  },
});

And then have a switch statement or something in the @require'd script to perform according to that custom event.

Would this be an adequate solution? Are there better options?

0 Answers0