markuspeloquin

google_voice_number_poll.user.js

Jul 18th, 2011
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name    Google Voice number poller
  3. // @description When logged in to Google Voice, polls for a new number within a set of area codes
  4. // @include https://www.google.com/voice*
  5. // @namespace   http://pages.cs.wisc.edu/~markus/util
  6. // @resource    jquery  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
  7. // ==/UserScript==
  8. // note on above: need to go to google.com/voice so as to be logged in ...
  9. // probably?
  10.  
  11. /* Copyright (c) 2011--2013, Markus Peloquin <markus@cs.wisc.edu>
  12.  *
  13.  * Permission to use, copy, modify, and/or distribute this software for any
  14.  * purpose with or without fee is hereby granted, provided that the above
  15.  * copyright notice and this permission notice appear in all copies.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  18.  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  20.  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  21.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  22.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  23.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  24.  
  25. eval(GM_getResourceText('jquery'));
  26.  
  27. $(function(){
  28.  
  29. var config = {
  30.     area_codes: [ 800, 808 ],
  31.     notify_script_url:
  32.         'http://hostname/cgi-bin/bluh.cgi',
  33.     retry_interval: 900, // (seconds)
  34.     send_email: true,
  35. };
  36.  
  37. function hash_empty(hash)
  38. {
  39.     for (var k in hash) return false;
  40.     return true;
  41. }
  42.  
  43. function timestamp()
  44. {
  45.     function push2(a, n)
  46.     {
  47.         if (n < 10) a.push('0');
  48.         a.push(n);
  49.     }
  50.     var now = new Date();
  51.     var parts = [ now.getFullYear(), '-' ];
  52.     push2(parts, now.getMonth()+1);
  53.     parts.push('-');
  54.     push2(parts, now.getDate());
  55.     parts.push(' ');
  56.     push2(parts, now.getHours());
  57.     parts.push(':');
  58.     push2(parts, now.getMinutes());
  59.     parts.push(':');
  60.     push2(parts, now.getSeconds());
  61.     return parts.join('');
  62. }
  63.  
  64. function check(ctx)
  65. {
  66.     var area_code = config.area_codes[ctx.i];
  67.     $.ajax({
  68.         url: 'https://www.google.com/voice/setup/searchnew/?ac=' +
  69.         area_code + '&start=0',
  70.         data: '',
  71.         dataType: 'json',
  72.         error: function (xhr, text_status, error_thrown) {
  73.         alert('error checking area code ' + area_code + ': ' +
  74.             text_status);
  75.         // the script stops completely
  76.         },
  77.         success: function (data, text_status, xhr) {
  78.         query_returned(data, text_status, xhr, ctx);
  79.         },
  80.     });
  81. }
  82.  
  83. function query_returned(data, textStatus, xhr, ctx)
  84. {
  85.     // just some assertions
  86.     if (!data) { alert('no data'); return }
  87.     if (!data.JSON) { alert('no JSON'); return }
  88.     if (data.JSON.num_matches == null) { alert('no num_matches'); return }
  89.  
  90.     var num_matches = Number(data.JSON.num_matches);
  91.     if (num_matches)
  92.         ctx.found[config.area_codes[ctx.i]] = num_matches;
  93.     ctx.i++;
  94.  
  95.     if (ctx.i == config.area_codes.length) {
  96.         console.info(timestamp(), data.JSON);
  97.         if (!hash_empty(ctx.found))
  98.             submit_results(ctx.found);
  99.         else {
  100.             // keep going
  101.             ctx.i = 0;
  102.             // try again in 15 minutes (as per default)
  103.             setTimeout(function(){ check(ctx) },
  104.                 config.retry_interval * 1000);
  105.             console.info(timestamp() + ': nope');
  106.         }
  107.     } else
  108.         // try again without waiting
  109.         check(ctx);
  110. }
  111.  
  112. function submit_results(found)
  113. {
  114.     var pairs = [];
  115.     for (var area_code in found)
  116.         pairs.push(area_code + ',' + found[area_code]);
  117.     var url = config.notify_script_url + '?found=' + pairs.join('+');
  118.  
  119.     console.info(timestamp() + ': found: ' + pairs.join(' -- '));
  120.     console.info(url);
  121.  
  122.     if (config.send_email) {
  123.         $.ajax({
  124.             url: url,
  125.             error: function (xhr, text_status, error_thrown) {
  126.             console.info(timestamp() + ': expected error submitting results: ' + text_status);
  127.             },
  128.             success: function (data, text_status, xhr) {
  129.             alert('all done; found: ' + found.join(' '));
  130.             },
  131.         });
  132.     }
  133.  
  134.     // try again in 15 minutes (as per default)
  135.     setTimeout(function(){ check({i: 0, found: {}}) },
  136.         config.retry_interval * 1000);
  137. }
  138.  
  139. setTimeout(function(){ check({i: 0, found: {}}) },
  140.     config.retry_interval * 1000);
  141.  
  142. });
Add Comment
Please, Sign In to add comment