Guest User

BrickLink Store2Catalog Greasemonkey

a guest
Feb 2nd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           BrickLink Store2Catalog Link
  3. // @namespace      bricklink
  4. // @description    Adds a link from store pages to the catalog page for an item
  5. // @include        http://www.bricklink.com/storeDetail.asp*
  6. // ==/UserScript==
  7.  
  8. // Kian Wright
  9.  
  10. function NSResolver(prefix) {
  11.   return 'http://www.w3.org/1999/xhtml';
  12. }
  13.  
  14. function xqry(query) {
  15.   var newq;
  16.   if (document.documentElement.namespaceURI) {
  17.     newq = '//x:' + query;
  18.   } else {
  19.     newq = '//'+query;
  20.   }
  21.   return newq;
  22. }
  23.  
  24. function xpath(query) {
  25.     return document.evaluate(query, document, NSResolver,
  26.         XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  27. }
  28.  
  29. function xp1st(query) {
  30.     return document.evaluate(query, document, NSResolver,
  31.         XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  32. }
  33.  
  34. // Add a link to the given element
  35. function createLink(href, text) {
  36.   var link = document.createElement('a');
  37.   link.setAttribute('href', href);
  38.   link.appendChild(document.createTextNode(text));
  39.   return link;
  40. }
  41.  
  42. var DEBUGNODE;
  43.  
  44. function debug(text) {
  45.   DEBUGNODE.innerHTML += text;
  46.   DEBUGNODE.innerHTML += '<br/>';
  47. }
  48.  
  49. function addCatalogLink(parent, itemID) {
  50.   var lnk = createLink('/catalogItem.asp?P='+itemID, 'Catalog');
  51.   parent.appendChild(document.createTextNode(' : '));
  52.   parent.appendChild(lnk);
  53. }
  54.  
  55. function addPriceGuideLink(parent, itemID, colorID) {
  56.   var lnk = createLink('/catalogPG.asp?P='+itemID+'&colorID='+colorID, 'PriceGuide');
  57.   parent.appendChild(document.createTextNode(' : '));
  58.   parent.appendChild(lnk);
  59. }
  60.  
  61. function getItemID(section) {
  62.   var xres = document.evaluate("x:a[contains(@href,'storeDetail.asp?b')]",
  63.     section, NSResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  64. //  debug(section.innerHTML + ': ' + xres.innerHTML);
  65.   if (xres != null) {
  66.     return xres.innerHTML;
  67.   } else
  68.     return null;
  69. }
  70.  
  71. var COLOR_REGEX = /img.bricklink.com\/.?\/(\d+)\//;
  72.  
  73. function getColorID(section) {
  74.   var row = section.parentNode.parentNode;
  75. //  debug('row: '+row);
  76.   var imgsrc = document.evaluate("x:td/x:a[contains(@id,'imgLink')]/x:img/@src",
  77. //  var imgsrc = document.evaluate("x:td/x:a[contains(@id,'imgLink')]",
  78.     row, NSResolver, XPathResult.STRING_TYPE, null);
  79. //  debug(section.innerHTML + ': ' + xres.innerHTML);
  80. //debug('image src: '+imgsrc);
  81.   if (imgsrc != null) {
  82.     var cid = COLOR_REGEX.exec(imgsrc.stringValue);
  83. //    debug('color id match: '+ cid);
  84.     if (cid != null)
  85.       return cid[1];
  86.     else
  87.       return null;
  88.   } else
  89.     return null;
  90. }
  91.  
  92. function addLinks() {
  93.   var xres = xpath("//x:a[text()='All Store Items']");
  94.   var itemID, colorID;
  95. //  debug('num of evaluate results: '+xres.snapshotLength);
  96.   if (xres != null && xres.snapshotLength > 0) {
  97.     for (var i=0; i<xres.snapshotLength; i++) {
  98.       itemID = getItemID(xres.snapshotItem(i).parentNode);
  99.       colorID = getColorID(xres.snapshotItem(i).parentNode);
  100.       addCatalogLink(xres.snapshotItem(i).parentNode, itemID);
  101.       if (colorID != null)
  102.         addPriceGuideLink(xres.snapshotItem(i).parentNode, itemID, colorID);
  103.     }
  104.   }
  105. }
  106.  
  107. function setupDebug() {
  108.   var e = document.getElementsByTagName('body')[0];
  109.   var d = document.createElement('div');
  110.   d.appendChild(document.createTextNode(''));
  111.   e.insertBefore(d, e.firstChild);
  112.   return d;
  113. }
  114.  
  115. function main() {
  116.   DEBUGNODE = setupDebug();
  117. //  debug('debug is set up');
  118.   addLinks();
  119. }
  120.  
  121. if (document.URL.indexOf('bricklink.com/storeDetail.asp') == -1)
  122.   GM_log('Active against unexpected URL ' + document.URL);
  123. else
  124.   main();
Advertisement
Add Comment
Please, Sign In to add comment