forked from segabito/ZenzaWatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZenzaBlogPartsButton.user.js
89 lines (80 loc) · 2.18 KB
/
ZenzaBlogPartsButton.user.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
// ==UserScript==
// @name ZenzaBlogPartsButton
// @namespace https://github.com/segabito/
// @description ニコニコ動画のブログパーツにZenzaWatch起動用ボタンを追加
// @match *://ext.nicovideo.jp/thumb/*
// @grant none
// @author segabito macmoto
// @license public domain
// @version 0.0.2
// ==/UserScript==
(function() {
const addStyle = function(styles, id) {
const elm = document.createElement('style');
//window.setTimeout(function() {
elm.type = 'text/css';
if (id) { elm.id = id; }
var text = styles.toString();
text = document.createTextNode(text);
elm.appendChild(text);
var head = document.getElementsByTagName('head');
head = head[0];
head.appendChild(elm);
//}, 0);
return elm;
};
const postMessage = function(type, message, token) {
const origin = document.referrer;
try {
parent.postMessage(JSON.stringify({
id: 'ZenzaWatch',
type: type, // '',
body: {
token: token,
url: location.href,
message: message
}
}),
origin);
} catch (e) {
alert(e);
console.log('err', e);
}
};
const __css__ = (`
#zenzaButton {
position: fixed;
left: 0;
top: 0;
z-index: 10000;
line-height: 24px;
padding: 4px 4px;
cursor: pointer;
font-weight: bolder;
display: none;
}
body:hover #zenzaButton {
display: inline-block;
}
`).trim();
const blogPartsApi = function() {
const watchId = location.href.split('/').reverse()[0];
const parentHost = document.referrer.split('/')[2];
if (!parentHost.match(/^[a-z0-9]*\.nicovideo\.jp$/)) {
window.console.log('disable bridge');
return;
}
addStyle(__css__);
const button = document.createElement('button');
button.innerHTML = '<span>Zen</span>';
button.id = 'zenzaButton';
document.body.appendChild(button);
button.onclick = (e) => {
postMessage('blogParts', {
command: e.shiftKey ? 'send' : 'open',
watchId: watchId
});
};
};
blogPartsApi();
})();