eensyweensy

a poor man's webspider
git clone https://wehaveforgeathome.hates.computer/eensyweensy.git
Log | Files | Refs | LICENSE

eensyweensy.user.js (2253B)


      1 // ==UserScript==
      2 // @name           Eensy Weensy
      3 // @namespace      example.com
      4 // @include        http://borderstylo.com/posts*
      5 // ==/UserScript==
      6 
      7 var Spider = function () {
      8   // defaults
      9   this.jquery = 'http://jquery.com/src/jquery-latest.js';
     10   this.home = 'http://localhost/eensyweensy/eensyweensy.php';
     11   this.grabber = function ($) { return {}; };
     12 };
     13 
     14 Spider.prototype.insert_jquery = function () {
     15   /* source: http://joanpiedra.com/jquery/greasemonkey/ */
     16   var GM_JQ = document.createElement('script');
     17   GM_JQ.src = this.jquery;
     18   GM_JQ.type = 'text/javascript';
     19   document.getElementsByTagName('head')[0].appendChild(GM_JQ);
     20   var that = this;
     21   function GM_wait () {
     22     if (typeof unsafeWindow.jQuery == 'undefined') {
     23       window.setTimeout(GM_wait, 100);
     24     }
     25     else {
     26       var $ = unsafeWindow.jQuery;
     27       that.grab_data($);
     28     }
     29   }
     30   GM_wait();
     31 };
     32 
     33 Spider.prototype.grab_data = function ($) {
     34   var data = this.grabber($);
     35   this.phone_home(data);
     36 };
     37 
     38 Spider.prototype.phone_home = function (data) {
     39   /* source: http://diveintogreasemonkey.org/api/gm_xmlhttprequest.html */
     40   GM_xmlhttpRequest({
     41     method: 'POST',
     42     headers: {
     43       "Content-Type": "application/x-www-form-urlencoded"
     44     },
     45     data: "data=" + JSON.stringify(data),
     46     url: this.home,
     47     onload: this.handle_results,
     48   });
     49 };
     50 
     51 Spider.prototype.handle_results = function (response) {
     52   var data;
     53   // JSON.parse barfs top-level errors on failure
     54   try {
     55     data = JSON.parse(response.responseText);
     56   }
     57   catch (e) {
     58     alert("debugging party:\n" + JSON.stringify(response));
     59     return;
     60   }
     61   if (data.error) { alert(data.error); return; }
     62   if (!data.url) { alert("no error, but no url to go to"); return; }
     63 
     64   document.location.assign(data.url);
     65 };
     66 
     67 Spider.prototype.go = function () {
     68   this.insert_jquery();
     69 };
     70 
     71 // our particular spider
     72 var spider = new Spider();
     73 spider.grabber = function ($) {
     74   var urls = [];
     75   $('.post-footer > a:nth-child(2)').each(function (i, node) {
     76     urls.push(node.href);
     77   });
     78   var next_page_button = $('.next_page');
     79   var on_last_page = next_page_button.hasClass('disabled');
     80   var data = {
     81     href: document.location.href,
     82     urls: urls,
     83     on_last_page: on_last_page,
     84   };
     85   return data;
     86 };
     87 spider.go();