-
Notifications
You must be signed in to change notification settings - Fork 67
/
DollchanAPI_Test.user.js
72 lines (69 loc) · 2.11 KB
/
DollchanAPI_Test.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
// ==UserScript==
// @name DollchanAPI Test
// @namespace DollchanAPI
// @version 0.2
// @author Dollchan Extension Tools Team
// @include *
// ==/UserScript==
function getDollchanAPI() {
return new Promise((resolve, reject) => {
const dw = document.defaultView;
const onmessage = ({ data, ports }) => {
if(ports && ports.length === 1 && data === 'de-answer-api-message') {
clearTimeout(to);
dw.removeEventListener('message', onmessage);
resolve(ports[0]);
}
};
dw.addEventListener('message', onmessage);
dw.postMessage('de-request-api-message', '*');
const to = setTimeout(() => {
dw.removeEventListener('message', onmessage);
reject();
}, 5e3);
});
}
function runAPI() {
getDollchanAPI().then(port => {
port.onmessage = ({ data }) => {
const result = data.data;
switch(data.name) {
case 'expandmedia': {
const ext = result.split('.').pop();
console.log(ext + ' is opened:', result);
if(ext === 'webm' || ext == 'mp4') {
console.log(document.querySelector(`video[src="${ result }"]`));
} else {
console.log(document.querySelector(`img[src="${ result }"]`));
}
break;
}
case 'filechange':
console.log('Files changed:', result);
break;
case 'newpost':
console.log('New posts detected:', result);
break;
case 'registerapi':
for(const key in result) {
console.log(`API ${ key } ${ result[key] ? 'is' : 'not' } registered.`);
}
break;
case 'submitform':
if(result.success) {
console.log('Post submitted succesfully!', result.num || '');
} else {
console.log('Post submitting error!', result.error);
}
break;
/* case '...': */
}
};
port.postMessage({ name: 'registerapi', data: ['expandmedia'] });
port.postMessage({ name: 'registerapi', data: ['filechange'] });
port.postMessage({ name: 'registerapi', data: ['newpost'] });
port.postMessage({ name: 'registerapi', data: ['submitform'] });
/* port.postMessage({ name: 'registerapi', data: ['...'] }); */
}).catch(() => console.log('Dollchan API not detected!'));
}
setTimeout(runAPI, 0);