Advertisement
Guest User

New Version

a guest
Jul 13th, 2011
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           BNScript: BlogNomic Utility Script
  3. // @namespace      blognomic
  4. // @description    Original by Sparrow, vote-counting added by alethiophile, updates by lilomar, and idler tracking by coppro
  5. // @include        http://*blognomic.com/
  6. // @include        http://*blognomic.com/archive/*
  7. // @include        http://*blognomic.com/blognomic/archive/*
  8. // @include        http://*blognomic.com/blognomic/
  9. // @require        http://jqueryjs.googlecode.com/files/jquery-1.2.3.pack.js
  10. // ==/UserScript==
  11.  
  12. var settings_default = {
  13.   'showNew': true,
  14.   'showTotal': true,
  15.   'debug': false,
  16.   'emperor': '',
  17.   'colorPassed': '#00cc00',
  18.   'colorFailed': '#ff0000',
  19.   'colorNew': '#ff0000'
  20. };
  21.  
  22. var form_data =
  23.   'Show new comments: <input type="checkbox" name="showNew" /><br />' +
  24.   'Show vote totals: <input type="checkbox" name="showTotal" /><br />' +
  25.   'Debug mode: <input type="checkbox" name="debug" /><br />' +
  26.   'Current Emperor: <input type="text" name="emperor" /><br />' +
  27.   'Color of passed counts: <input type="text" name="colorPassed" /><br />' +
  28.   'Color of failed counts: <input type="text" name="colorFailed" /><br />' +
  29.   'Color of new comments: <input type="text" name="colorNew" /><br />' +
  30.   '<button type="button">Save</button>';
  31.  
  32. inputs = document.getElementsByTagName("input");
  33. for (i=0; i<inputs.length; ++i) {
  34.   if(inputs[i].name == "notify_me") inputs[i].checked = false;
  35. }
  36.  
  37. // This whole block from http://umkk.eu/wp-content/uploads/2009/10/json.js
  38. var JSON=JSON||{};(
  39.   function() {
  40.     function f(n) {
  41.       return n<10 ? '0' + n : n;
  42.     }
  43.     if (typeof Date.prototype.toJSON !== 'function') {
  44.       Date.prototype.toJSON = function(key) {
  45. return isFinite(this.valueOf()) ? this.getUTCFullYear() + '-' +
  46. f(this.getUTCMonth()+1) + '-' + f(this.getUTCDate()) + 'T' +
  47. f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' +
  48. f(this.getUTCSeconds()) + 'Z' : null;
  49.       };
  50.       String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function(key) {
  51. return this.valueOf();
  52.       }
  53.     }
  54.     var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;
  55.     function quote(string) {
  56.       escapable.lastIndex=0;
  57.       return escapable.test(string) ? '"' +
  58. string.replace(escapable, function(a) {
  59.    var c=meta[a];
  60.    return typeof c === 'string' ? c : '\\u' +
  61.      ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  62.  }) + '"' : '"' + string + '"'
  63. }
  64.     function str(key,holder) {
  65.       var i, k, v, length, mind=gap, partial, value=holder[key];
  66.       if (value && typeof value === 'object' &&
  67.  typeof value.toJSON === 'function') {
  68. value=value.toJSON(key);
  69.       }
  70.       if (typeof rep === 'function') {
  71. value = rep.call(holder,key,value);
  72.       }
  73.       switch (typeof value) {
  74.       case'string':
  75. return quote(value);
  76.       case'number':
  77. return isFinite(value) ? String(value) : 'null';
  78.       case'boolean':
  79.       case'null':
  80. return String(value);
  81.       case'object':
  82. if(!value) {
  83.  return'null';
  84. }
  85. gap += indent;
  86. partial=[];
  87. if (Object.prototype.toString.apply(value) === '[object Array]') {
  88.  length=value.length;
  89.  for (i=0; i<length; i+=1) {
  90.    partial[i] = str(i,value) || 'null';
  91.  }
  92.  v = partial.length === 0 ? '[]' : gap ? '[\n' + gap +
  93.    partial.join(',\n' + gap) + '\n' + mind + ']' : '[' +
  94.    partial.join(',') + ']';
  95.  gap = mind;
  96.  return v;
  97. }
  98. if (rep && typeof rep === 'object') {
  99.  length = rep.length;
  100.  for(i=0; i<length; i+=1) {
  101.    k=rep[i];
  102.    if(typeof k === 'string') {
  103.      v=str(k,value);
  104.      if (v) {
  105. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  106.      }
  107.    }
  108.  }
  109. }
  110. else {
  111.  for(k in value) {
  112.    if (Object.hasOwnProperty.call(value,k)) {
  113.      v = str(k,value);
  114.      if (v) {
  115. partial.push(quote(k) + (gap ? ': ' : ':') + v);
  116.      }
  117.    }
  118.  }
  119. }
  120. v = partial.length === 0 ? '{}' : gap ? '{\n' + gap +
  121.  partial.join(',\n' + gap) + '\n' + mind + '}' : '{' +
  122.  partial.join(',') + '}';
  123. gap = mind;
  124. return v;
  125.       }
  126.     }
  127.     if (typeof JSON.stringify !== 'function') {
  128.       JSON.stringify = function(value,replacer,space) {
  129. var i;
  130. gap = '';
  131. indent = '';
  132. if (typeof space === 'number') {
  133.  for (i=0; i<space; i+=1) {
  134.    indent+=' ';
  135.  }
  136. }
  137. else if (typeof space === 'string') {
  138.  indent = space;
  139. }
  140. rep = replacer;
  141. if (replacer && typeof replacer !== 'function' &&
  142.    (typeof replacer !== 'object' || typeof replacer.length !==
  143.     'number')) {
  144.  throw new Error('JSON.stringify');
  145. }
  146. return str('',{'':value});
  147.       }
  148.     }
  149.     if (typeof JSON.parse !== 'function') {
  150.       JSON.parse = function(text,reviver) {
  151. var j;
  152. function walk(holder,key) {
  153.  var k, v, value=holder[key];
  154.  if (value && typeof value === 'object') {
  155.    for (k in value) {
  156.      if (Object.hasOwnProperty.call(value,k)) {
  157. v = walk(value,k);
  158. if (v!==undefined) {
  159.  value[k] = v;
  160. }
  161. else {
  162.  delete value[k];
  163. }
  164.      }
  165.    }
  166.  }
  167.  return reviver.call(holder,key,value);
  168. }
  169. cx.lastIndex=0;
  170. if (cx.test(text)) {
  171.  text=text.replace(cx,function(a) {
  172.      return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
  173.    });
  174. }
  175. if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,'')))
  176. {
  177.  j = eval('(' + text + ')');
  178.  return typeof reviver === 'function' ? walk({'':j},'') : j;
  179. }
  180. throw new SyntaxError('JSON.parse');
  181.       }
  182.     }
  183.   }());
  184. // End borrowed code
  185.  
  186. var settings;
  187.  
  188. function getComments(text) {
  189.   return parseInt(text.replace(/[^0-9]/g, ''))
  190. }
  191.  
  192. function set(k,v) { // hack
  193.   window.setTimeout(function() {
  194.     GM_setValue(k,v)
  195.   }, 0)
  196. }
  197.  
  198. function get(k,v, callback) { // hack
  199.   window.setTimeout(function() {
  200.     callback(GM_getValue(k,v))
  201.   }, 0)
  202. }
  203.  
  204. function mainPage() {
  205.   $("<button type=\"button\">BNScript settings</button>").prependTo("#sidebar2").click(settings_query);
  206.   $("a[title='Comments']").each(function(i) {
  207.     var link = this
  208.    
  209.     get(this.href, false, function(old_comments) {
  210.       var num_comments = getComments(link.text)
  211.       var num_new_comments = num_comments - (old_comments || 0)
  212.      
  213.       if ((!old_comments && num_comments > 0) || num_comments > old_comments) {
  214.         $(link).append(' <span style="color: ' + settings['colorNew'] + '">(' + num_new_comments + ' new)</span>')
  215.       }
  216.      
  217.       $(link).click(function() {
  218.         set(link.href, num_comments)
  219.       })
  220.      
  221.       if (settings['showNew']) {
  222.         if (num_new_comments > 0) {
  223.           $('#header').append('<a href="' + link.href + '" style="color: ' + settings['colorNew'] + '">' + (num_comments - old_comments) + '</a> ')
  224.         } else {
  225.          $('#header').append('0 ')
  226.         }
  227.       }
  228.     })
  229.   })
  230. }
  231.  
  232. function archivePage() {
  233.   $("a[title='Comments']").each(function(i) {
  234.       set(this.href, getComments(this.text));
  235.     });
  236.   var votes = new Object();
  237.   var proposer;
  238.   proposer = $("p[class=post-footer]").children().filter("em").
  239.     children().filter("a").html();
  240.   var isProposal;
  241.   isProposal = /^Proposal\:/.test($("h3[class^=post-title]").html());
  242.   var quorum;
  243.   quorum = $("#PlayerList").next().html();
  244.   quorum = /\D*(\d+)\D*(\d+)/.exec(quorum);
  245.   var numPlayers = parseInt(quorum[1]);
  246.   quorum = parseInt(quorum[2]);
  247.   GM_log('Number of players: ' + numPlayers);
  248.   GM_log('Quorum: ' + quorum);
  249.   var failQuorum = quorum - 1 + numPlayers % 2;
  250.   GM_log('Failure quorum: ' + failQuorum);
  251.   $("div[class='comment']").
  252.     each(
  253.       function(i) {
  254. var comment = this;
  255. var name = $(comment).children().filter('h3').children().
  256.  filter('a').html();
  257. var cbody = $(comment).children().filter('div.commentbody');
  258. if (settings['debug']) {
  259.  GM_log('In vote-counter: voter #' + i + ': ' + name);
  260. }
  261. $(cbody).find("img").each(
  262.  function(j) {
  263.         var cb = this;
  264.         var vote = cb.getAttribute('alt');
  265.      if (settings['debug']) {
  266.           GM_log('In vote-counter: vote #' + j + ': ' +
  267.              vote);
  268.    }
  269.             if (vote == 'for' || vote == 'against' ||
  270.                 (vote == 'imperial' && isProposal) ||
  271.                 (vote == 'veto' && isProposal && settings[emperor] != '' && name == settings[emperor]))
  272.               votes[name] = vote;
  273.             else if (settings['debug'])
  274.               GM_log('Vote not counted');
  275.  });
  276.       });
  277.   var players = {};
  278.   $("#PlayerList > a").each(function(i) {
  279.       players[$(this).text().replace(/\*$/, "")] = true;
  280.   });
  281.   var numFor = 0;
  282.   var numAgainst = 0;
  283.   var numImp = 0;
  284.   var emperor = '';
  285.   var veto = false;
  286.   var sk = false;
  287.   if (votes[proposer] == undefined && isProposal && players[proposer])
  288.     votes[proposer] = 'for';
  289.   if (settings['debug']) {
  290.     GM_log('In vote-counter: Vote count: ' + JSON.stringify(votes));
  291.     GM_log('In vote-counter: Proposer: ' + proposer);
  292.     GM_log('In vote-counter: isProposal: ' + isProposal);
  293.   }
  294.   for (key in votes) {
  295.     if (!players[key])
  296.       continue;
  297.     if (settings[emperor] != '' && key == settings['emperor'])
  298.       emperor = votes[key];
  299.     if (votes[key] == 'for')
  300.       numFor++;
  301.     if (votes[key] == 'against')
  302.       numAgainst++;
  303.     if (votes[key] == 'imperial')
  304.       numImp++;
  305.   }
  306.   if (isProposal && emperor == 'veto')
  307.     veto = true;
  308.   if (isProposal && (votes[proposer] == 'against' || (votes[proposer] == 'imperial' && emperor == 'against')))
  309.     sk = true;
  310.   if (settings['debug']) {
  311.     GM_log('In vote-counter: emperor\'s vote: ' + emperor);
  312.     GM_log('In vote-counter: vote numbers: ' + numFor + ' ' +
  313.   numAgainst + ' ' + numImp);
  314.     GM_log('In vote-counter: veto/s-k: ' + veto + '/' + sk);
  315.   }
  316.   var foradj = numFor, agadj = numAgainst, impadj = numImp;
  317.   if (emperor == 'for') {
  318.     foradj += impadj;
  319.     impadj = 0;
  320.   }
  321.   if (emperor == 'against') {
  322.     agadj += impadj;
  323.     impadj = 0;
  324.   }
  325.   var sstring;
  326.   sstring = "text-align: left";
  327.   if (foradj >= quorum)
  328.     sstring += '; color: ' + settings['colorPassed'];
  329.   if (agadj >= failQuorum || veto || sk)
  330.     sstring += '; color: ' + settings['colorFailed'];
  331.   var pstring;
  332.   if (veto)
  333.     pstring = '<span style="' + sstring + '">Vetoed; votes: ';
  334.   else if (sk)
  335.     pstring = '<span style="' + sstring + '">Self-killed; votes: ';
  336.   else
  337.     pstring = '<span style="' + sstring + '">Votes: ';
  338.   if (emperor != '' && numImp != 0 && emperor != 'imperial') {
  339.     pstring += foradj + '-' + agadj + ' (' + numFor + '-' + numAgainst +
  340.       '-' + numImp +')</span>';
  341.   }
  342.   else {
  343.     pstring += foradj + '-' + agadj +
  344.       (impadj != 0 ? '-' + impadj : '') + '</span>';
  345.   }
  346.   if (settings['showTotal'])
  347.     $("#main2").prepend(pstring);
  348. }
  349.  
  350. function settings_query(event) {
  351.   var dbody = document.body;
  352.   var bgcolor = $(dbody).css("background-color");
  353.   var form;
  354.   GM_log('At start: ' + JSON.stringify(settings));
  355.   $(dbody).empty();
  356.   $(dbody).css("background-color", bgcolor);
  357.   $(dbody).append('<form></form>');
  358.   form = $(dbody).children().filter('form');
  359.   $(form).submit(function () {
  360.       settings_save($(this));
  361.       return false;
  362.     });
  363.   $(form).append(form_data);
  364.   if (settings['showNew'])
  365.     $(form).children().filter('[name=showNew]').attr('checked', 'checked');
  366.   if (settings['showTotal'])
  367.     $(form).children().filter('[name=showTotal]').attr('checked', 'checked');
  368.   if (settings['debug'])
  369.     $(form).children().filter('[name=debug]').attr('checked', 'checked');
  370.   $(form).children().filter('[name=emperor]').
  371.     attr('value', settings['emperor']);
  372.   $(form).children().filter('[name=colorPassed]').
  373.     attr('value', settings['colorPassed']);
  374.   $(form).children().filter('[name=colorFailed]').
  375.     attr('value', settings['colorFailed']);
  376.   $(form).children().filter('[name=colorNew]').
  377.     attr('value', settings['colorNew']);
  378.   $(form).children().filter('button').click(function (event) {
  379.       settings_save($(this).parent());
  380.     });
  381. }
  382.  
  383. function settings_save(form) {
  384.   settings['showNew'] = form.children().filter('[name=showNew]').
  385.     attr('checked') == true ? true : false;
  386.   settings['showTotal'] = form.children().filter('[name=showTotal]').
  387.     attr('checked') == true ? true : false;
  388.   settings['debug'] = form.children().filter('[name=debug]').
  389.     attr('checked') == true ? true : false;
  390.   settings['emperor'] = form.children().filter('[name=emperor]').attr('value');
  391.   settings['colorPassed'] = form.children().filter('[name=colorPassed]').
  392.     attr('value');
  393.   settings['colorFailed'] = form.children().filter('[name=colorFailed]').
  394.     attr('value');
  395.   settings['colorNew'] = form.children().filter('[name=colorNew]').
  396.     attr('value');
  397.   GM_log(JSON.stringify(settings));
  398.   set("settings", JSON.stringify(settings));
  399.   unsafeWindow.location.reload();
  400. }
  401.      
  402. var setstr = GM_getValue("settings");
  403.  
  404. $(document).ready(function() {
  405.     if (setstr)
  406.       settings = JSON.parse(setstr);
  407.     else
  408.       settings = settings_default;
  409.     if (/^http:\/\/(www\.)?blognomic.com\/(blognomic\/)?archive\/.*$/.
  410. test(location.href)) {
  411.       archivePage();
  412.     }
  413.     else if (/^http:\/\/(www\.)?blognomic.com\/(blognomic\/)?$/.
  414.     test(location.href)) {
  415.       mainPage();
  416.     }
  417.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement