-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
123rf.js
51 lines (39 loc) · 1.1 KB
/
123rf.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
import {validateUrl} from 'utils/app';
import {runOnce} from 'utils/common';
import {initSearch, prepareImageForUpload, sendReceipt} from 'utils/engines';
const engine = '123rf';
async function search({session, search, image, storageIds}) {
image = await prepareImageForUpload({
image,
engine,
target: 'api'
});
const data = new FormData();
data.append('image_base64', image.imageDataUrl);
const rsp = await fetch(
'https://www.123rf.com/apicore/search/reverse/upload',
{
mode: 'cors',
method: 'POST',
body: data
}
);
if (rsp.status !== 200) {
throw new Error(`API response: ${rsp.status}, ${await rsp.text()}`);
}
const rspText = await rsp.text();
// JSON response may start with HTML
const searchData = JSON.parse(rspText.substring(rspText.indexOf('{')));
const tabUrl =
'https://www.123rf.com/reverse-search/?fid=' + searchData.data.fid;
await sendReceipt(storageIds);
if (validateUrl(tabUrl)) {
window.location.replace(tabUrl);
}
}
function init() {
initSearch(search, engine, taskId);
}
if (runOnce('search')) {
init();
}