forked from Zhaph/BrickJax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrickJax-1.0.3.user.js
More file actions
177 lines (139 loc) · 6.14 KB
/
BrickJax-1.0.3.user.js
File metadata and controls
177 lines (139 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ==UserScript==
// @name BrickJax
// @namespace http://brickjax.doodle.co.uk/
// @description Supplies Brick images for http://bricks.stackexchange.com/ - Version 1.0.4
// @include http://bricks.stackexchange.com/*
// @include http://meta.bricks.stackexchange.com/*
// @include http://chat.stackexchange.com/rooms/1741/*
// @include http://chat.stackexchange.com/rooms/1653/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js
// @author Kevin Cathcart and @Zhaph
// ==/UserScript==
/*
* BrickJax v0.12.3
* Copyright (c) 2011 Kevin Cathcart
* Designed to supply brick images for http://bricks.stackexchange.com/
* Based on content containing [part:partid:colorid]
* Ben Duguid: Added Peeron, BrickLink and BrickSet links for set details
*
* The searchText functions based on:
* jQuery replaceText
* Copyright (c) 2009 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function ($) { $.fn.replaceText = function (b, a, c) { return this.each(function () { var f = this.firstChild, g, e, d = []; if (f) { do { if (f.nodeType === 3) { g = f.nodeValue; e = g.replace(b, a); if (e !== g) { if (!c && /</.test(e)) { $(f).before(e); d.push(f) } else { f.nodeValue = e } } } } while (f = f.nextSibling) } d.length && $(d).remove() }) } })(jQuery);
var brickJax = (function ($) {
"use strict";
var brickJax = {};
function log(obj) {
if (window.console) {
console.log(obj);
};
GM_log(obj);
};
function buildImage(ajaxData) {
log("Entered buildImage");
var text = '<img style="max-height:100px;" src="' + ajaxData.Src + '" alt="' + ajaxData.AltText + '" />';
return text;
}
function buildLink(ajaxData) {
log("Entered buildLink");
var text = '<a href="http://www.peeron.com/inv/parts/' + ajaxData.PartId + '">' + ajaxData.BrickName;
if (ajaxData.BrickName.indexOf(ajaxData.PartId) == -1) {
text += " (" + ajaxData.PartId + ")";
}
text += ' <img style="max-height:100px;" src="' + ajaxData.Src + '" alt="in ' + ajaxData.ColourName + '" /></a>';
return text;
}
function replaceSet(node, str, number, colour) {
log("Entered replaceSet");
log(str, number, colour, node);
var text = '<a href="http://www.peeron.com/inv/sets/' + number + '-1">' + number + ' on Peeron</a>';
$(node).replaceText(str, text);
}
function replaceBrickSet(node, str, number, colour) {
log("Entered replaceBrickSet");
log(str, number, colour, node);
var text = '<a href="http://www.brickset.com/detail/?Set=' + number + '-1">' + number + ' on BrickSet</a>';
$(node).replaceText(str, text);
}
function replaceBrickLink(node, str, number, colour) {
log("Entered replaceBrickLink");
log(str, number, colour, node);
var text = '<a href="http://www.bricklink.com/catalogItem.asp?S=' + number + '-1">' + number + ' on BrickLink</a>';
$(node).replaceText(str, text);
}
function replaceImage(node, str, number, colour) {
log("Entered replaceImage");
log(str, number, colour, node);
getBricksForImage(number, colour, node, str);
}
function replaceLink(node, str, number, colour) {
log("Entered replaceLink");
log(str, number, colour, node);
getBricksForLink(number, colour, node, str);
}
function searchText(elements, regex, toInvoke) {
log("Entered searchText");
elements.each(function () {
var parent = this, node = this.firstChild;
// Only continue if firstChild exists.
if (node) {
// Loop over all childNodes.
do {
// Only process text nodes.
if (node.nodeType === 3) {
node.nodeValue.replace(regex, function (str) {
var args = Array.prototype.slice.call(arguments);
args.unshift(parent);
log(args);
toInvoke.apply(null, args);
return str;
});
}
} while (node = node.nextSibling);
}
});
}
function replaceTags(element) {
log("Entered replaceTags");
var elements = $(element).find('*').andSelf();
elements = elements.not($('script,noscript,style,textarea,pre,code').find('*').andSelf());
searchText(elements, /\[part:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceImage);
searchText(elements, /\[partlink:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceLink);
searchText(elements, /\[set:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceSet);
searchText(elements, /\[bs:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceBrickSet);
searchText(elements, /\[bl:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceBrickLink);
log("Leaving replaceTags");
};
function getBricksForImage(number, colour, node, str) {
log("Entered getBricksForImage");
GM_xmlhttpRequest({
method: "POST",
url: "http://brickjax.doodle.co.uk/bricks.aspx/JsonDetails/" + number + "/" + colour,
dataType: "jsonp",
onload: function (data) {
$(node).replaceText(str, buildImage($.parseJSON(data.responseText)));
}
});
};
function getBricksForLink(number, colour, node, str) {
log("Entered getBricksForLink");
GM_xmlhttpRequest({
method: "POST",
url: "http://brickjax.doodle.co.uk/bricks.aspx/JsonDetails/" + number + "/" + colour,
dataType: "jsonp",
onload: function (data) {
$(node).replaceText(str, buildLink($.parseJSON(data.responseText)));
}
});
};
brickJax.buildLink = buildLink;
brickJax.buildImage = buildImage;
brickJax.getBricksForImage = getBricksForImage;
brickJax.getBricksForLink = getBricksForLink;
brickJax.replaceTags = replaceTags;
return brickJax;
})(jQuery);
brickJax.replaceTags($('body'));