Advertisement
Guest User

Old Version

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