-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (49 loc) · 1.6 KB
/
index.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
(function(d) {
// development
// var base = 'http://127.0.0.1:8000/me/bookmarks';
// online
var base = '//yuehu.io/me/bookmarks';
if (!d.getElementById('yuehu-bookmark-style')) {
var link = d.createElement('link');
link.id = 'yuehu-bookmark-style';
link.rel = 'stylesheet';
var t = parseInt(new Date().valueOf() / 100000);
link.href = '//dn-yuehu.qbox.me/assets/bookmark.css?t=' + t;
d.body.appendChild(link);
}
var notice = d.createElement('div');
notice.className = 'yuehu-bookmark-notice';
notice.innerHTML = '<div class="yuehu-bookmark-loading"><span class="dot dot-0"></span><span class="dot dot-1"></span><span class="dot dot-2"></span></div>';
d.body.appendChild(notice);
// find page url
var pageURL = location.href;
var links = d.getElementsByTagName('link');
for (var i=0; i<links.length; i++) {
if (links[i].getAttribute('rel') === 'canonical') {
pageURL = links[i].getAttribute('href');
}
}
if (pageURL.charAt(0) === '/') {
pageURL = location.hostname + pageURL;
}
if (!/^https?\:\/\//.test(pageURL)) {
pageURL = 'http://' + pageURL;
}
var script = d.createElement('script');
script.src = base + '?via=bookmark&url=' + pageURL;
// bookmark callback
window._yuehu_bookmark = function(resp) {
if (resp.location) {
location.href = resp.location;
} else {
notice.innerHTML = resp.message;
setTimeout(function() {
if (notice.parentNode) {
notice.parentNode.removeChild(notice);
}
}, 2000);
}
d.body.removeChild(script);
};
d.body.appendChild(script);
})(document);