freecog.net

novelistlinkfix.user.js

Also see Install.

   1 // ==UserScript==
   2 // @name           NoveList Link Fixer
   3 // @namespace      http://freecog.net/2006/
   4 // @description    Turns javascript: links on EBSCO NoveList into real ones, so that they can be middle-clicked, etc.
   5 // @include        http://novelst*.epnet.com*
   6 // @version        0.3
   7 // ==/UserScript==
   8 
   9 
  10 function res_value(name) {
  11     return encodeURIComponent(document.forms.namedItem('results')
  12            .elements.namedItem(name).value);
  13 }
  14 
  15 
  16 function jump_to_real_url(s) {
  17     s = s.replace(/<\/?b>/g, '');
  18     s = s.replace(/=(The|An|Or|And|Not)\+/, '=');
  19     var sid = res_value('sid');
  20     if (s.indexOf('rid=') > -1) {
  21         return 'results.aspx?sid=' + sid + '&control=tr&' + s;
  22     } else if (s.indexOf('author=') > -1) {
  23         return 'results.aspx?sid=' + sid +
  24             '&control=br&searchtype=qbs&tag1=ST&prox=-3&' + s;
  25     } else {
  26         return 'results.aspx?sid=' + sid + 
  27             '&control=br&from=treedoc&searchtype=tree&' + s;
  28     }
  29 }
  30 
  31 function details_to_real_url(js) {
  32     var m = js.match(/Detail\(\s*'(\d+)',\s*'(\d+)'\s*\)/);
  33     ui = m[1];
  34     hitnum = m[2];
  35     return 'results.aspx?control=bd&sid=' + res_value('sid')
  36         + '&booleantext=' + res_value('ctl1:booleanText')
  37         + '&fuzzytext=' + res_value('ctl1:fuzzyText')
  38         + '&hitnum=' + hitnum + '&ui=' + ui
  39         + '&prox=' + res_value('ctl1:prox')
  40         + '&sort=' + res_value('ctl1:sort')
  41         + '&totalhits=' + res_value('ctl1:totalHits')
  42         + '&starthit=' + res_value('ctl1:startHit')
  43         + '&displayText=' + res_value('ctl1:displayText')
  44         + '&frm=' + res_value('frm');
  45 }
  46 
  47 var unhandled = 0;
  48 var a, as = Array.slice(document.getElementsByTagName('a'));
  49 while ((a = as.pop())) {
  50     if (a.href.indexOf('javascript:jumpToURL(') == 0) {
  51         var q = a.href.slice(22, -3);
  52         a.href = jump_to_real_url(q);
  53     } else if (a.href.match(/^javascript:Detail\(/i)) {
  54         a.href = details_to_real_url(a.href);
  55     } else if (a.href.match(/javascript:openDialog\(/i)) {
  56         a.setAttribute('onclick', a.getAttribute('href').slice(11) + " return false;");
  57         var m = a.href.match(/openDialog\(('.*?')/);
  58         try {
  59             a.href = m[1].slice(1, -1);
  60         } catch(e) {
  61             GM_log('Error matching openDialog: ' + e);
  62         }
  63     } else if (a.href.match(/javascript:/) &&
  64      !a.href.match(/^javascript:AddOneRec\(/) && // The search "folder" is
  65      !a.href.match(/^javascript:AddAllRecs\(/)) { // totally JavaScript, and can't be fixed
  66         GM_log('Unhandled link! -- ' + a.href);
  67         unhandled++;
  68     }
  69 }
  70 
  71 if (unhandled) {
  72     GM_log('***  The NoveList Link Fixer found ' + unhandled + ' javascript link(s)');
  73     GM_log('***  that it can\'t handle.  Please notify me at');
  74     GM_log('***  http://freecog.net/contact/');
  75 }
  76 
  77 
  78 // Replace triple question marks in Publishers Weekly reviews
  79 // with em dashes.
  80 const EM_DASH = '\u2014';
  81 const REPLACE = /\?\?\?/g;
  82 
  83 function replace_triples(el) {
  84     var n, ns = Array.slice(el.childNodes);
  85     while (n = ns.pop()) {
  86         if (n.nodeType == 1) { // Element
  87             replace_triples(n);
  88         } else if (n.nodeType == 3) {
  89             n.nodeValue = n.nodeValue.replace(REPLACE, EM_DASH);
  90         } // else: pass
  91     }
  92 }
  93 
  94 var p, ps = Array.slice(document.getElementsByTagName('p'));
  95 while ((p = ps.pop())) {
  96     if (p.textContent.match(/Publishers Weekly/i)) {
  97         replace_triples(p);
  98         break;
  99     }
 100 }
 101 
 102 // Changes:
 103 // 0.3 - "???" now converted em dashes on Publishers Weekly reviews
 104 // 0.2 - "details" links now fixed
 105 // 0.1 - Initial release