forked from onassar/JS-Pinterest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pinterest.js
182 lines (163 loc) · 5.25 KB
/
Pinterest.js
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
178
179
180
181
/**
* Pinterest
*
* Extends the Pinterest-button-code to allow re-running (is that a word?) it
* again.
*
* Useful if you have ajax content being loaded in, or are creating Pinterest
* buttons dynamically, and want to make them fully-functional.
*
* @author Oliver Nassar <[email protected]>
* @see <http://github.com/onassar/JS-Pinterest>
* @see <http://web.onassar.com/blog/2012/10/16/github-project-js-pinterest/>
* @see <http://pinterest.com/about/goodies/>
* @see <http://assets.pinterest.com/js/pinit.js>
* @note I didn't make any actual changes to the Pinterest code, just cleaned
* it up a bit and commented it for my own sake of :)
*/
var Pinterest = (function() {
/**
* _args
*
* @private
* @var Array
*/
var _args = [
document,
window,
{
att: {
layout: "count-layout",
count: "always-show-count"
},
pinit: "http://pinit-cdn.pinterest.com/pinit.html",
pinit_secure: "https://assets.pinterest.com/pinit.html",
button: "//pinterest.com/pin/create/button/?",
vars: {
req: ["url", "media"],
opt: ["title", "description"]
},
layout: {
none: {
width: 43,
height: 20
},
vertical: {
width: 43,
height: 58
},
horizontal: {
width: 90,
height: 20
}
}
}
];
/**
* _core
*
* @private
* @param m
* @param q
* @param c
* @return void
*/
var _core = (function(m, q, c) {
var s = function(h) {
//
var d = c.pinit,
n = "?",
a, i, f, b;
f = [];
b = [];
//
var j = {},
g = m.createElement("IFRAME"),
r = h.getAttribute(c.att.count) || false,
o = h.getAttribute(c.att.layout) || "horizontal";
//
if (q.location.protocol === "https:") d = c.pinit_secure;
f = h.href.split("?")[1].split("#")[0].split("&");
a = 0;
//
for (i = f.length; a < i; a += 1) {
b = f[a].split("=");
j[b[0]] = b[1]
}
//
a = f = 0;
for (i = c.vars.req.length; a < i; a += 1) {
b = c.vars.req[a];
if (j[b]) {
d = d + n + b + "=" + j[b];
n = "&"
}
f += 1
}
//
if (j.media && j.media.indexOf("http") !== 0) f = 0;
//
if (f === i) {
a = 0;
//
for (i = c.vars.opt.length; a < i; a += 1) {
b = c.vars.opt[a];
if (j[b]) d = d + n + b + "=" + j[b]
}
//
d = d + "&layout=" + o;
d = d + "&ref=" + encodeURIComponent(m.URL);
//
if (r !== false) d += "&count=1";
//
g.setAttribute("src", d);
g.setAttribute("scrolling", "no");
g.allowTransparency = true;
g.frameBorder = 0;
g.style.border = "none";
g.style.width = c.layout[o].width + "px";
g.style.height = c.layout[o].height + "px";
h.parentNode.replaceChild(g, h)
}
else {
h.parentNode.removeChild(h)
}
},
//
p = m.getElementsByTagName("A"),
l, e, k = [];
/**
* Pushes all anchors into the <k> array, presumably since
* <getElementsByTagName> doesn't return an actual array, but just that
* weird Elements object type, or whatever it is
*/
e = 0;
for (l = p.length; e < l; e += 1) {
k.push(p[e]);
}
/**
* Loops through all the anchors, and runs the replacement function <s>
* against it if the <href> value contains the string
* <//pinterest.com/pin/create/button/?>
*
* @note At first I thought I would need to do a check against a button
* to see if it'd already been transformed, but it seems this
* library removes the node from the DOM (see line #135), so
* we're good.
*/
e = 0;
for (l = k.length; e < l; e += 1) {
k[e].href
&& k[e].href.indexOf(c.button) !== -1
&& s(k[e]);
}
});
// return singelton
return {
init: function() {
_core.apply(window, _args);
}
}
})();
// run (to mirror existing Pinterest js functionality)
Pinterest.init();