This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
pinboard-particular.js
244 lines (213 loc) · 6.29 KB
/
pinboard-particular.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(function() {
/******************* begin configuration options ***************************/
// Change `read` to true to invoke the promptless, self-closing version of the
// bookmarklet.
var readlater = false;
var appUrl = null;
// When set to true, selected text is quoted using <blockquote>.
// Note that Markdown is not supported in link descriptions because of an XSS
// vulnerability: https://twitter.com/Pinboard/status/22436355472625664
var quoteSelection = false;
// When this text appears in title or description, they are added as tags.
var tagKeywords = {
javascript:'javascript',
js:'javascript',
python:'python',
ios:'ios',
youtube:'video',
vimeo:'video',
video:'video',
books:'book',
book:'book'
};
// this matches domain names to special selectors for the title
var titleTweaks = {
"github.com":".entry-title .js-current-repository"
};
// this matches domain names to special selectors for the title
var descriptionTweaks = {
"www.kickstarter.com":".short-blurb"
};
// limit long titles and descriptions, mostly to avoid 'HTTP/1.0 414 Request URI too long'
var textLengthLimit = 1000;
/********************* begin code ********************************************/
// reduce a string to some canonical representation
// right now this just picks a case but could get really complicated if need be
// see: http://stackoverflow.com/questions/227950/programatic-accent-reduction-in-javascript-aka-text-normalization-or-unaccentin
// some people like stack overflow straighten their curly quotes
var normalize = function(string) {
return string.toLowerCase();
};
var elementText = function(el) {
return el ? el.textContent.trim().replace(/\s+/g,' ') : null;
};
var normalizedDocumentTitle = normalize(document.title);
// used as tes
var isSubtitle = function(string) {
if(string) {
return normalizedDocumentTitle.indexOf(normalize(string)) !== -1;
}
else {
return false;
}
};
// loops over a node list and applies a function
// returning the first value that is non-null
var selectFromNodeList = function(nodeList,func,thisObj) {
thisObj = thisObj || window;
var l = nodeList.length;
var result;
for(var i=0;i<l;++i) {
result = func.call(thisObj,nodeList[i]);
if(result !== null) {
return result;
}
}
return null;
};
var getTitle = function() {
var url = location.href;
var host = location.hostname;
var e;
if(host in titleTweaks) {
e = document.querySelector(titleTweaks[host]);
if(e) {
return elementText(e);
}
}
var documentTitle = document.title;
e = document.querySelector("meta[property='og:title']");
if(e) {
documentTitle = e.content.trim().replace(/\s+/g,' ');
}
// hEntry microformat
if(selectFromNodeList(document.getElementsByClassName('hentry'), function() {return true;})) {
var htitle = document.querySelector(
'.hentry .entry-title'
);
if(htitle) {
return elementText(htitle);
}
}
// method 1 - look for link to self with text that is contained in title
var a_text = selectFromNodeList(document.getElementsByTagName('A'), function(a) {
if(a.href === url) {
a_text = elementText(a);
if(isSubtitle(a_text)) {
return a_text;
}
}
return null;
});
if(a_text) {
return a_text;
}
// method 2 - look at header tags and see if it matches part of title
var headerTags = ['h1','h2','h3','h4','h5','h6'];
var headerTitle;
for(var j=0;j<headerTags.length;++j) {
selectFromNodeList(document.getElementsByTagName(headerTags[j]), function(h) {
var h_text = elementText(h);
if(isSubtitle(h_text) && (!headerTitle || h_text.length > headerTitle.length)) {
headerTitle = h_text;
}
return null;
});
}
if(headerTitle) {
return headerTitle;
}
// method 3 - just return the title
return documentTitle;
};
var getTags = function(text) {
text = normalize(text);
var tags = [];
var re;
for(var keyword in tagKeywords) {
re = keyword instanceof RegExp ? keyword : new RegExp("\\b"+keyword+"\\b","i");
if(re.test(text)) {
tags.push(tagKeywords[keyword]);
}
}
return tags;
};
var getMetaDescription = function() {
var e;
e = document.querySelector("meta[name='description']");
if(e) {
return e.content.trim().replace(/\s+/g,' ');
}
e = document.querySelector("meta[property='og:description']");
if(e) {
return e.content.trim().replace(/\s+/g,' ');
}
return "";
};
var getDescription = function() {
var text;
// Grab the text selection (if any) and quote it
if('' !== (text = String(document.getSelection()))) {
if(quoteSelection) {
text = text.trim().split("\n").map(function(s) {return "<blockquote>"+s+"</blockquote>";}).join("\n");
}
}
var host = location.hostname;
var e;
if(host in descriptionTweaks) {
e = document.querySelector(descriptionTweaks[host]);
if(e) {
return elementText(e);
}
}
if(!text) {
text = getMetaDescription();
}
return text;
};
// Assembles default form pre-fill arguments.
var url = location.href;
var title = getTitle();
var description = getDescription();
// remove if title is trailing or leading
var ix = description.indexOf(title);
if(ix === 0) {
description = description.substring(title.length).trim();
}
else if(ix === description.length-title.length) {
description = description.substring(0,ix).trim();
}
var tags = getTags(document.title+" "+description+" "+getMetaDescription());
if(textLengthLimit > 0) {
title = title.substring(0, textLengthLimit);
description = description.substring(0, textLengthLimit);
}
var args = [
'url=', encodeURIComponent(url),
'&title=', encodeURIComponent(title),
'&description=', encodeURIComponent(description),
'&tags=', encodeURIComponent(tags.join(" "))
];
// If readlater mode, add the auto-close parameter and read-later flag:
if(readlater) {
args = args.concat([
'&later=', 'yes',
'&jump=', 'close'
]);
}
if(appUrl) {
args = args.concat([
'&x-source=Safari',
'&x-success=',encodeURIComponent(location.href),
'&x-cancel=',encodeURIComponent(location.href)
]);
window.location = appUrl+args.join('');
}
else {
var pin = open('http://pinboard.in/add?'+args.join(''), 'Pinboard', 'toolbar=no,width=610,height=350');
// Send the window to the background if readlater mode.
if(readlater) {
pin.blur();
}
}
})();