SHOW:
|
|
- or go back to the newest paste.
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 |
8 | + | |
9 | function NSResolver(prefix) { | |
10 | return 'http://www.w3.org/1999/xhtml'; | |
11 | } | |
12 | ||
13 | function xqry(query) { | |
14 | var newq; | |
15 | if (document.documentElement.namespaceURI) { | |
16 | newq = '//x:' + query; | |
17 | } else { | |
18 | newq = '//'+query; | |
19 | } | |
20 | return newq; | |
21 | } | |
22 | ||
23 | function xpath(query) { | |
24 | return document.evaluate(query, document, NSResolver, | |
25 | XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
26 | } | |
27 | ||
28 | function xp1st(query) { | |
29 | return document.evaluate(query, document, NSResolver, | |
30 | XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; | |
31 | } | |
32 | ||
33 | // Add a link to the given element | |
34 | function createLink(href, text) { | |
35 | var link = document.createElement('a'); | |
36 | link.setAttribute('href', href); | |
37 | link.appendChild(document.createTextNode(text)); | |
38 | return link; | |
39 | } | |
40 | ||
41 | var DEBUGNODE; | |
42 | ||
43 | function debug(text) { | |
44 | DEBUGNODE.innerHTML += text; | |
45 | DEBUGNODE.innerHTML += '<br/>'; | |
46 | } | |
47 | ||
48 | function addCatalogLink(parent, itemID) { | |
49 | var lnk = createLink('/catalogItem.asp?P='+itemID, 'Catalog'); | |
50 | parent.appendChild(document.createTextNode(' : ')); | |
51 | parent.appendChild(lnk); | |
52 | } | |
53 | ||
54 | function addPriceGuideLink(parent, itemID, colorID) { | |
55 | var lnk = createLink('/catalogPG.asp?P='+itemID+'&colorID='+colorID, 'PriceGuide'); | |
56 | parent.appendChild(document.createTextNode(' : ')); | |
57 | parent.appendChild(lnk); | |
58 | } | |
59 | ||
60 | // Returns string item ID for the given section (or null) | |
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 | //Returns string color ID for the given section (or null) | |
74 | function getColorID(section) { | |
75 | var row = section.parentNode.parentNode; | |
76 | // debug('row: '+row); | |
77 | var imgsrc = document.evaluate("x:td/x:a[contains(@id,'imgLink')]/x:img/@src", | |
78 | // var imgsrc = document.evaluate("x:td/x:a[contains(@id,'imgLink')]", | |
79 | row, NSResolver, XPathResult.STRING_TYPE, null); | |
80 | // debug(section.innerHTML + ': ' + xres.innerHTML); | |
81 | //debug('image src: '+imgsrc); | |
82 | if (imgsrc != null) { | |
83 | var cid = COLOR_REGEX.exec(imgsrc.stringValue); | |
84 | // debug('color id match: '+ cid); | |
85 | if (cid != null) | |
86 | return cid[1]; | |
87 | else | |
88 | return null; | |
89 | } else | |
90 | return null; | |
91 | } | |
92 | ||
93 | // Add hyperlinks to each store item listed on the page | |
94 | function addLinks() { | |
95 | var xres = xpath("//x:a[text()='All Store Items']"); | |
96 | var itemID, colorID; | |
97 | // debug('num of evaluate results: '+xres.snapshotLength); | |
98 | if (xres != null && xres.snapshotLength > 0) { | |
99 | for (var i=0; i<xres.snapshotLength; i++) { | |
100 | // Get information about this store item | |
101 | itemID = getItemID(xres.snapshotItem(i).parentNode); | |
102 | colorID = getColorID(xres.snapshotItem(i).parentNode); | |
103 | // Add a catalog link for this store item | |
104 | addCatalogLink(xres.snapshotItem(i).parentNode, itemID); | |
105 | if (colorID != null) | |
106 | // if the color ID exists, add a price guide link | |
107 | // note - this will fail for minifigs, as they don't have a color | |
108 | // TODO - fix for minifigs | |
109 | addPriceGuideLink(xres.snapshotItem(i).parentNode, itemID, colorID); | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | // create a div at the top of the page for debug output | |
115 | function setupDebug() { | |
116 | var e = document.getElementsByTagName('body')[0]; | |
117 | var d = document.createElement('div'); | |
118 | d.appendChild(document.createTextNode('')); | |
119 | e.insertBefore(d, e.firstChild); | |
120 | return d; | |
121 | } | |
122 | ||
123 | function main() { | |
124 | DEBUGNODE = setupDebug(); | |
125 | // uncomment the next line to enable debug output | |
126 | // debug('debug is set up'); | |
127 | addLinks(); | |
128 | } | |
129 | ||
130 | if (document.URL.indexOf('bricklink.com/storeDetail.asp') == -1) | |
131 | // Greasemonkey has called us for a page that we didn't expect | |
132 | GM_log('Active against unexpected URL ' + document.URL); | |
133 | else | |
134 | main(); |