Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Google Voice number poller
- // @description When logged in to Google Voice, polls for a new number within a set of area codes
- // @include https://www.google.com/voice*
- // @namespace http://pages.cs.wisc.edu/~markus/util
- // @resource jquery http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
- // ==/UserScript==
- // note on above: need to go to google.com/voice so as to be logged in ...
- // probably?
- /* Copyright (c) 2011--2013, Markus Peloquin <markus@cs.wisc.edu>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
- * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
- eval(GM_getResourceText('jquery'));
- $(function(){
- var config = {
- area_codes: [ 800, 808 ],
- notify_script_url:
- 'http://hostname/cgi-bin/bluh.cgi',
- retry_interval: 900, // (seconds)
- send_email: true,
- };
- function hash_empty(hash)
- {
- for (var k in hash) return false;
- return true;
- }
- function timestamp()
- {
- function push2(a, n)
- {
- if (n < 10) a.push('0');
- a.push(n);
- }
- var now = new Date();
- var parts = [ now.getFullYear(), '-' ];
- push2(parts, now.getMonth()+1);
- parts.push('-');
- push2(parts, now.getDate());
- parts.push(' ');
- push2(parts, now.getHours());
- parts.push(':');
- push2(parts, now.getMinutes());
- parts.push(':');
- push2(parts, now.getSeconds());
- return parts.join('');
- }
- function check(ctx)
- {
- var area_code = config.area_codes[ctx.i];
- $.ajax({
- url: 'https://www.google.com/voice/setup/searchnew/?ac=' +
- area_code + '&start=0',
- data: '',
- dataType: 'json',
- error: function (xhr, text_status, error_thrown) {
- alert('error checking area code ' + area_code + ': ' +
- text_status);
- // the script stops completely
- },
- success: function (data, text_status, xhr) {
- query_returned(data, text_status, xhr, ctx);
- },
- });
- }
- function query_returned(data, textStatus, xhr, ctx)
- {
- // just some assertions
- if (!data) { alert('no data'); return }
- if (!data.JSON) { alert('no JSON'); return }
- if (data.JSON.num_matches == null) { alert('no num_matches'); return }
- var num_matches = Number(data.JSON.num_matches);
- if (num_matches)
- ctx.found[config.area_codes[ctx.i]] = num_matches;
- ctx.i++;
- if (ctx.i == config.area_codes.length) {
- console.info(timestamp(), data.JSON);
- if (!hash_empty(ctx.found))
- submit_results(ctx.found);
- else {
- // keep going
- ctx.i = 0;
- // try again in 15 minutes (as per default)
- setTimeout(function(){ check(ctx) },
- config.retry_interval * 1000);
- console.info(timestamp() + ': nope');
- }
- } else
- // try again without waiting
- check(ctx);
- }
- function submit_results(found)
- {
- var pairs = [];
- for (var area_code in found)
- pairs.push(area_code + ',' + found[area_code]);
- var url = config.notify_script_url + '?found=' + pairs.join('+');
- console.info(timestamp() + ': found: ' + pairs.join(' -- '));
- console.info(url);
- if (config.send_email) {
- $.ajax({
- url: url,
- error: function (xhr, text_status, error_thrown) {
- console.info(timestamp() + ': expected error submitting results: ' + text_status);
- },
- success: function (data, text_status, xhr) {
- alert('all done; found: ' + found.join(' '));
- },
- });
- }
- // try again in 15 minutes (as per default)
- setTimeout(function(){ check({i: 0, found: {}}) },
- config.retry_interval * 1000);
- }
- setTimeout(function(){ check({i: 0, found: {}}) },
- config.retry_interval * 1000);
- });
Add Comment
Please, Sign In to add comment