WonderLand WonderLand - 5 months ago 17
Javascript Question

Jquery callback - Uncaught ReferenceError

I think the below problem is scope related but I'm not very good with jQuery plugin pattern, how can I solve it ?


I'm trying to fix an issue (Cross Domain) with this chrome extension

I think I found a solution ( using

) but I got an new problem with the

Uncaught ReferenceError: jQuery19109374020271279013_1462700430014 is not defined:

?url=https%3A//thepiratebay.cr/search/I%20Will%20Survive%201999/0/7/0&callback=jQuery19109374020271…:1 Uncaught ReferenceError: jQuery19109374020271279013_1462700430016 is not defined(anonymous function) @ ?url=https%3A//thepiratebay.cr/search/I%20Will%20Survive%201999/0/7/0&callback=jQuery19109374020271…:1

Here the code involved
is where I go the issue:

(function($) {
tpbSearch : function(options){
var defaults = { searchTerm: ''};
var options = $.extend(defaults, options);
return this.each(function(){
doSearch($(this), options.searchTerm);

// private functions
// ....

function buildSearchUrl(searchTerm)
var searchPart = 'https://thepiratebay.cr/search/' + encodeURIComponent(searchTerm) + '/0/7/0';
var searchUrl = 'http://anyorigin.com/get?url=' + escape(searchPart) + '&callback=?';

return searchUrl;
// function where the issue happens.
function doSearch(container, searchTerm){
// here we should iterate over the container selector, because it's possible multiple items will be selected.
var sanitizedSearchTerm = sanitizeSearchTerm(searchTerm);

// set the default logo
var loadingImageUrl = chrome.extension.getURL('images/ajax-loader.gif');
var logo = $('<img id="tpb-logo">')
.attr('src', loadingImageUrl)
.css('width', '25px')
.css('height', '25px');

var searchUrl = buildSearchUrl(sanitizedSearchTerm);

$.getJSON(searchUrl, function(data){

Xan Xan

That's not the right way to do it from a Chrome extension anyway.

If you run into cross-origin problems, you need to define host permissions for that origin. Then Chrome will ignore CORS. See Requesting cross-origin permissions in Google documentation.

For completeness, though I discourage you from fixing this:

Your error here is treating JSONP in a content script. JSONP works by inserting a <script> element, but that executes in page's context while the callback is in a separate content script context. This gives the error of being unable to find the callback function.

Using JSONP is really awkward in content scripts as a result and if ABSOLUTELY needed should be delegated to the background page (with appropriate CSP considerations).