I'm writing a Safari Extension that adds some functionality to a web page that I don't own. I would like for my injected JavaScript page to be able to fire some JavaScript functions the host page uses, but it doesn't work. On the Console I get the message ReferenceError: Can't find variable: function_name
.
My script is specified to be an End Script, so the whole page should be loaded. The function is also called from an onclick()
handler on the page, like so:
onclick="function_name($(this).up());"
I can get a reference to that page element, but when I call element.onclick()
I get another error: TypeError: 'undefined' is not a function (evaluating '$(this).up()')
.
Oddly, when I call the JavaScript function from an AppleScript (do JavaScript "function_name()" in page
) it works fine. How can I trigger these functions?
It's not working because the extension's injected script is sandboxed; it can't see the page's global objects except for the DOM (and vice versa). A way around this security limitation is to have the injected script create a
<script>
element with the statements you want and insert it into the document. For example:However, the inserted script will not have access to the injected script's objects either. So, for example, if you try to access the extension's
safari
object from the inserted script, you will get a reference error.I can extend answer from canisbos. You can communicate with the inserted script with the PostMessage function.
injected script:
Test html page: