Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Extension injected scripts cannot access the web page's JavaScript namespace. Your injected script creates a <code>&lt;script&gt;</code> element and adds it to the page's DOM, but then the <code>jQuery</code> object instantiated by the script belongs to the page's namespace, not to your injected script's.</p> <p>There are at least two potential solutions. One, inject jQuery into the page the normal way, using the extension API. This method is only viable if the web pages that you are targeting can be categorized using URL patterns.</p> <p>Two, use <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage" rel="noreferrer"><code>Window.postMessage</code></a> to communicate between your injected script and the web page's namespace. You will need to add another <code>&lt;script&gt;</code> to the page, and in this script, have a listener for the <code>message</code> event. The listener will be able to use jQuery as if it were "native" to the page itself.</p> <p>Here's some code to get you started, if needed.</p> <p>In the extension injected script:</p> <pre><code>var s0 = document.createElement('script'); s0.type = 'text/javascript'; s0.src = 'https://code.jquery.com/jquery-2.0.3.min.js'; document.head.appendChild(s0); var s1 = document.createElement('script'); s1.type = 'text/javascript'; s1.src = safari.extension.baseURI + 'bridge.js'; document.head.appendChild(s1); window.addEventListener('message', function (e) { if (e.origin != window.location.origin) return; console.log(e.data); }, false); window.postMessage('What jQuery version?', window.location.origin); </code></pre> <p>In bridge.js:</p> <pre><code>window.addEventListener('message', function (e) { if (e.origin != window.location.origin) return; if (e.data == 'What jQuery version?') { e.source.postMessage('Version ' + $.fn.jquery, window.location.origin); } }, false); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload