CategoryJsFiddle

How to fiddle on any web page

Once you have a nice jQuery plugin for accessing any URL, it’s only natural that you start fiddling around with some web page here and there.

Lately I wanted to dominate the http://disco-tools.eu/disco2_portal/terms.php beast.. ehm.. page, which is home to the official European thesaurus of skills and competences. It’s a good starting point for projects related with education in Europe, but also worldwide. Even if it is far from perfect, it’s translated to many European languages (English, German, French, Czech, Hungarian, Italian, Lithuanian, Polish, Slovak, Spanish, Swedish) and it organizes around 9000 terms in each language.

Screen Shot 2014-06-05 at 11.13.33

My target is to be able to browse the thesaurus from the Result frame of jsFiddle.

First Attempt

I already showed that YQL is not the best choice for getting this URL, due to its inability to let stuff come through without correcting it before. So, I will still use my own proxy.

My first attempt (open in jsFiddle):

$(function() {
    
    $.proxyGet('http://disco-tools.eu/disco2_portal/terms.php', function(data) {
        
        $('body').append(data);
                
    }, my_proxy);

});


$.proxyGet=function(a,b,c){if(null!==a&&!/^(https?:|\/\/)/.test(a))throw new SyntaxError("Expected a URL.");a&&0===a.indexOf("//")&&(a=window.location.protocol+a);var d={url:"http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent('select * from html where url="--URL--" and compat="html5" and xpath="*"')+"&format=xml",cleanup:function(a){return(a=a.results&&a.results[0])?a=a.replace(/
/gi,"\r").replace(/
/gi,"\n"):null},filter:null};c=$.extend(d,c||{});var e=c.url.replace("--URL--",encodeURIComponent(a))+"&callback=?";$.getJSON(e,function(a){$.isFunction(b)&&($.isFunction(c.cleanup)&&(a=c.cleanup(a),$.isFunction(c.filter)&&(a=c.filter(a))),b(a))})};

var my_proxy = {
    url: 'http://andowebsit.es/proxy/?url=--URL--&code=...',
    cleanup: function (data) {
        data = data.result;
        return (data ? data : null);
    }
};

produced this result:

Screen Shot 2014-06-05 at 10.19.50

Pages: 1 2 3 4 5 6 7 8

How to include a jsFiddle into another jsFiddle

jsFiddle is an online editor that allows you to work on “proof of concepts” in HTML + CSS + JavaScript. You can import many different JavaScript libraries, like jQuery, and also any other unsupported ones, if they are reachable on the internet, for example through cdnjs.

One thing jsFiddle lacks is the ability to import stuff from another fiddle. It’s king of frustrating not to be able to do it, moreover when it’s coupled with the inability to upload stuff.

Here is a possible solution that will work at least for your own fiddles. The idea is to somehow mark the pieces of code you want to export. Something like this:

/*[[[[[*/
var test = 'some code';
/*]]]]]*/

function hypotenuse(a, b) {
    return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
$('#triple').html('The smallest Pythagorean triple is (3, 4, ' + hypotenuse(3, 4) + ').');

/*[[[[[*/
console.log('Just imported and executed ' + test + '.');
/*]]]]]*/

Then, in the fiddle you want to import to, you can use my $.proxyGet jQuery plugin to download your fiddle, extract those pieces, join them again and globally eval them. Something like this:

$(function(){
    
    $.proxyGet('http://jsfiddle.net/aercolino/jLDz9/show/', function (data) {$.globalEval(data);}, {
        filter: function(data) {
            var start   = '/*[[[[[*/'.replace(/\W/g, '\\$&');
            var end     = '/*]]]]]*/'.replace(/\W/g, '\\$&');
            var sections = [];
            data.replace(RegExp(start + '([\\s\\S]*?)' + end, 'g'), function(all, section) {
                sections.push(section);
            });
            return sections.join("\n;\n");
        }
    });
    
});

$.proxyGet=function(a,b,c){if(null!==a&&!/^(https?:|\/\/)/.test(a))throw new SyntaxError("Expected a URL.");a&&0===a.indexOf("//")&&(a=window.location.protocol+a);var d={url:"http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent('select * from html where url="--URL--" and compat="html5" and xpath="*"')+"&format=xml",cleanup:function(a){return(a=a.results&&a.results[0])?a=a.replace(/
/gi,"\r").replace(/
/gi,"\n"):null},filter:null};c=$.extend(d,c||{});var e=c.url.replace("--URL--",encodeURIComponent(a))+"&callback=?";$.getJSON(e,function(a){$.isFunction(b)&&($.isFunction(c.cleanup)&&(a=c.cleanup(a),$.isFunction(c.filter)&&(a=c.filter(a))),b(a))})};

Which will dutifully show the following message in its console:

Just imported and executed some code.

© 2017 Notes Log

Theme by Anders NorenUp ↑