AJAX from within a Safari Extension's injected script

958 views Asked by At

I'm trying to expand a shorturl using an API in an injected script in a Safari Extension:

$.getJSON('http://api.longurl.org/v2/expand?format=json&url=' +  encodeURIComponent(href) + '&callback=?', function(data) {

    console.log(data);
});

And I'm getting the following error:

ReferenceError: Can't find variable: jQuery15103411371528636664_1298845652395

I've tried a different API and get the same error, so I know it is not that. Also, if I execute the same code from the console, I get a successful response. So it must be something to do with being inside the Safari Extension's injected script.

Any ideas?

1

There are 1 answers

1
Dovy On

I believe you have to add jQuery to the plugin first. Here's an example of how:

var newElement = document.createElement("script");
newElement.type = "text/javascript";
newElement.src = "//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
document.body.insertBefore(newElement, document.body.firstChild);

Then you can do jQuery in a tab using your plugin. ;)