Skip to content

Instantly share code, notes, and snippets.

@codeorelse
Created March 6, 2016 20:12
Show Gist options
  • Save codeorelse/3af02169ba12a2874d5b to your computer and use it in GitHub Desktop.
Save codeorelse/3af02169ba12a2874d5b to your computer and use it in GitHub Desktop.
Scraper Client JS
/*! bluemango-scraper - v0.2.24 - 2016-02-16 */
!function(a){"use strict";function b(){return window.location.origin||(window.location.origin=window.location.protocol+"//"+window.location.hostname+(window.location.port?":"+window.location.port:"")),window.location.href}function c(){return window.location.hash}function d(){return window.location.pathname}function e(){return window.location.origin}function f(a){var b=/^(?:[a-z]+:)?\/\//i;return b.test(a)}function g(b,c){var d=h(b.split("#")[0]),e=j.getOrigin()+j.getPathName(),f=[];return a.each(c,function(a,b){"number"==typeof a&&(a=b,b=c[a]);var e=d[a];e||(e=b),f.push(a+"="+e)}),e+"?"+f.join("&")}function h(a){for(var b,c={},d=a.slice(a.indexOf("?")+1).split("&"),e=0;e<d.length;e++)b=d[e].split("="),c[b[0]]=b[1];return c}var i={allowAllFieldNames:!1,debug:!1,fieldNames:["id","available","title","imageUrl","clickUrl","category","basket","description","priceNormal","priceDiscount","logoUrl","stickerText","custom1","custom2","custom3","custom4"]},j=function(b,c){this.fields=b,this.options=a.extend({},i,c),this.result={},this.errors=[]};j.getHash=c,j.getHref=b,j.getOrigin=e,j.getPathName=d,j.isAbsoluteUrl=f,j.modifyUrlWithParameters=g,a.extend(j.prototype,{getText:function(b){return a(b.selector).first().val()?a(b.selector).first().val():a(b.selector).first().text()},getOuterHtml:function(b){return a(b.selector).first().prop("outerHTML")},getMatches:function(a,b,c){c=c||1;for(var d,e=[];null!==(d=b.exec(a));)e.push(d[c]);return e},createRegexFromString:function(a){var b=a.replace(/.*\/([gimy]*)$/,"$1"),c=a.replace(new RegExp("^/(.*?)/"+b+"$"),"$1");return new RegExp(c,b)},filterFieldNames:function(b){var c={};return a.each(this.options.fieldNames,function(a,d){void 0!==b[d]&&(c[d]=b[d])}),c},getField:function(b){if(this.result[b])return this.result[b];var c,d=this,e=this.fields[b];if(void 0===e)return"";if("function"==typeof e)c=e(d);else switch(e.type){case"url":var f=j.getHref();e.query&&(f=j.modifyUrlWithParameters(f,e.query)),c=e.prefix?e.prefix+encodeURIComponent(f):f;break;case"image":c=a(e.selector).attr("src"),c&&!j.isAbsoluteUrl(c)&&(0!==c.indexOf("/")&&(c="/"+c),c=j.getOrigin()+c);break;case"dictionary":a.each(e.dictionary,function(a,b){var f=d.getOuterHtml(e),g=d.createRegexFromString(b);g.test(f)&&(c=a)});break;case"regex":var g=this.createRegexFromString(e.regex);c=this.getMatches(this.getText(e),g)[0];break;case"template":c=e.template.replace(/(\{\{.+?\}\})/g,function(a,b){var c=b.substr(2,b.length-4);return d.getField(c)});break;default:c=this.getText(e)}if(void 0!==c&&""!==c||e.required===!1||this.errors.push(b+": empty result"),e.test){var h=this.createRegexFromString(e.test);h.test(c)||this.errors.push(b+": does not match test")}return e.post&&(c=e.post(c)),c},getResults:function(){var b=this;return a.each(this.fields,function(a){b.result[a]=b.getField(a)}),this.errors.length>0&&this.options.debug?(this.log(this.errors.join("\n")),{}):(b.options.allowAllFieldNames||(b.result=b.filterFieldNames(b.result)),b.result)},log:function(a){window.logerror?logerror(a):"object"==typeof console&&"function"==typeof console.log&&console.log(a)}}),a.expr[":"].blank=function(b){return!a.trim(a(b).text()).length},window.Scraper=j}($||jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment