1

I've written a small userscript for Google Chrome. It works pretty fine, until I call a function initTimer() There is no such a function in my script, but it is in a script in the page on which my userscript runs, but anyway there's an error initTimer() is not defined. I've tried to write window.initTimer(), but it says Object [object DOMWindow] has no method 'initTimer'. So how can I make it work?
Thanks in advance

Cracker
  • 912
  • 2
  • 14
  • 26

1 Answers1

2

Because userscripts are typically sandboxed from the rest of the browser environment, userscripts cannot interact with the scripts running on the page itself, nor can scripts running on the page interact with userscripts, for security reasons.

You'll have to do script injection for this, by creating a script element in the page itself containing the code you want to execute.

var s = document.createElement('script');
s.innerHTML = 'initTimer();';
document.body.appendChild(s);

The problem with this, which may or may not break your script, is that the injected code will have no way of communicating directly with the code in the sandbox, so you'd either have to inject all of your code, or use an alternative method to communicate if you need to.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136