-
Notifications
You must be signed in to change notification settings - Fork 67
Dollchan API en
This script provides several APIs for external scripts and applications.
To register an API consumer, you need to add the following code to your script:
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 }) => {
switch(data.name) {
case 'registerapi':
for(let key in data.data) {
console.log(`API ${ key } ${
data.data[key] ? 'registered' : 'unavailable' }.`);
}
break;
case 'newpost':
console.log('New posts: ', data.data);
break;
/* case '...': */
}
};
port.postMessage({ name: 'registerapi', data: ['newpost'] });
/* port.postMessage({ name: 'registerapi', data: ['...'] }); */
}).catch(() => console.log('Old version of dollscript without API support.'));
}
setTimeout(runAPI, 0);
An onmessage
event in the runAPI
function will return a data
object with the following properties:
-
data.name
— event name, -
data.data
— data passed by an event.
If the dollscript is installed, after execution you would see a list of supported APIs in the console. For example,
API newpost registered.
If you have an old version of the dollscript or it is disabled, you would see:
Old version of dollscript without API support.
This event allows your application to monitor new posts added by the dollscript. API returns a data
property with an array of new posts' numbers. For example:
case 'newpost':
console.log('New posts: ', data.data);
/* your code */
break;
When new posts arrive, you would see something like this in the console:
New posts: Array [ 18649619, 18649619 ]
In place of console.log
add your own code to process data.data
array.
This event monitors jpg/png/gif/webm/mp4 expansion in post or by center. API returns a data
property with a src
attribute of an expanded picture/video. Example:
case 'expandmedia':
const src = data.data;
const ext = src.split('.').pop();
console.log(ext + ' expanded:', src);
/* your code */
break;
When an image/webm is expanded, you would see something like this in the console:
jpg expanded: /b/src/146459420/14869184607150.jpg
png expanded: /b/src/146574543/14869060587720.png
webm expanded: /b/src/146584803/14869150047100.webm
You can process selected content only by analyzing file extensions, for example:
if(ext === 'webm') {
const webmEl = document.querySelector(`video[src="${ src }"]`);
/* process webm file */
}
This event reports the result of attempting to post a replay or create a thread. The API will return a data
field with an Object containing the results. It may contain the following:
-
success
— equalstrue
on successful send, orfalse
otherwise, -
num
— post number (only on success and only if the image board engine supports tracking your posts), -
error
— error response upon failure
Example:
const result = data.data;
switch(data.name) {
case 'submitform':
console.log(result);
/* your code here */
break;
Possible results:
Object { success: true, num: 22098360 }
Object { success: true, num: null }
Object { success: false, error: "Error: wrong CAPTCHA." }
Object { success: false, error: "Error: You must type a message or attach a file." }