Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name BrickLink Store2Catalog Link
- // @namespace bricklink
- // @description Adds a link from store pages to the catalog page for an item
- // @include http://www.bricklink.com/storeDetail.asp*
- // ==/UserScript==
- // Kian Wright
- function NSResolver(prefix) {
- return 'http://www.w3.org/1999/xhtml';
- }
- function xqry(query) {
- var newq;
- if (document.documentElement.namespaceURI) {
- newq = '//x:' + query;
- } else {
- newq = '//'+query;
- }
- return newq;
- }
- function xpath(query) {
- return document.evaluate(query, document, NSResolver,
- XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
- }
- function xp1st(query) {
- return document.evaluate(query, document, NSResolver,
- XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- }
- // Add a link to the given element
- function createLink(href, text) {
- var link = document.createElement('a');
- link.setAttribute('href', href);
- link.appendChild(document.createTextNode(text));
- return link;
- }
- var DEBUGNODE;
- function debug(text) {
- DEBUGNODE.innerHTML += text;
- DEBUGNODE.innerHTML += '<br/>';
- }
- function addCatalogLink(parent, itemID) {
- var lnk = createLink('/catalogItem.asp?P='+itemID, 'Catalog');
- parent.appendChild(document.createTextNode(' : '));
- parent.appendChild(lnk);
- }
- function addPriceGuideLink(parent, itemID, colorID) {
- var lnk = createLink('/catalogPG.asp?P='+itemID+'&colorID='+colorID, 'PriceGuide');
- parent.appendChild(document.createTextNode(' : '));
- parent.appendChild(lnk);
- }
- function getItemID(section) {
- var xres = document.evaluate("x:a[contains(@href,'storeDetail.asp?b')]",
- section, NSResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- // debug(section.innerHTML + ': ' + xres.innerHTML);
- if (xres != null) {
- return xres.innerHTML;
- } else
- return null;
- }
- var COLOR_REGEX = /img.bricklink.com\/.?\/(\d+)\//;
- function getColorID(section) {
- var row = section.parentNode.parentNode;
- // debug('row: '+row);
- var imgsrc = document.evaluate("x:td/x:a[contains(@id,'imgLink')]/x:img/@src",
- // var imgsrc = document.evaluate("x:td/x:a[contains(@id,'imgLink')]",
- row, NSResolver, XPathResult.STRING_TYPE, null);
- // debug(section.innerHTML + ': ' + xres.innerHTML);
- //debug('image src: '+imgsrc);
- if (imgsrc != null) {
- var cid = COLOR_REGEX.exec(imgsrc.stringValue);
- // debug('color id match: '+ cid);
- if (cid != null)
- return cid[1];
- else
- return null;
- } else
- return null;
- }
- function addLinks() {
- var xres = xpath("//x:a[text()='All Store Items']");
- var itemID, colorID;
- // debug('num of evaluate results: '+xres.snapshotLength);
- if (xres != null && xres.snapshotLength > 0) {
- for (var i=0; i<xres.snapshotLength; i++) {
- itemID = getItemID(xres.snapshotItem(i).parentNode);
- colorID = getColorID(xres.snapshotItem(i).parentNode);
- addCatalogLink(xres.snapshotItem(i).parentNode, itemID);
- if (colorID != null)
- addPriceGuideLink(xres.snapshotItem(i).parentNode, itemID, colorID);
- }
- }
- }
- function setupDebug() {
- var e = document.getElementsByTagName('body')[0];
- var d = document.createElement('div');
- d.appendChild(document.createTextNode(''));
- e.insertBefore(d, e.firstChild);
- return d;
- }
- function main() {
- DEBUGNODE = setupDebug();
- // debug('debug is set up');
- addLinks();
- }
- if (document.URL.indexOf('bricklink.com/storeDetail.asp') == -1)
- GM_log('Active against unexpected URL ' + document.URL);
- else
- main();
Advertisement
Add Comment
Please, Sign In to add comment