This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
135 lines (114 loc) · 4.4 KB
/
lib.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const Twitter = (() => {
return class Twitter
{
constructor(consumerKey, consumerSecret, accessToken, accessTokenSecret)
{
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.accessToken = accessToken;
this.accessTokenSecret = accessTokenSecret;
}
get(url, params = {})
{
const callback = `jsonpCallback_${randomId()}`;
const components = buildComponents.call(this, 'GET', url, Object.assign({callback}, params));
const script = document.createElement('script');
return new Promise((resolve, reject) => {
document.body.appendChild(script);
script.src = `${url}?${buildQuery(components)}`;
script.addEventListener('error', reject);
window[callback] = resolve;
setTimeout(reject, 3000);
})
.then(json => {
document.body.removeChild(script);
delete window[callback];
return json;
})
.catch(e => {
document.body.removeChild(script);
delete window[callback];
throw {error: `${e ? 'Load' : 'Timeout'} Error (This is a dummy message)`};
});
}
post(url, params = {})
{
const target = `dummyIframe_${randomId()}`;
const components = buildComponents.call(this, 'POST', url, params);
const iframe = document.createElement('iframe');
const form = document.createElement('form');
iframe.name = target;
iframe.style.visibility = 'hidden';
iframe.style.width = 0;
iframe.style.height = 0;
form.action = url;
form.method = 'post';
form.target = target;
document.body.appendChild(iframe);
document.body.appendChild(form);
for (const [name, value] of Object.entries(components)) {
const input = document.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = value;
form.appendChild(input);
}
return new Promise((resolve, reject) => {
form.submit();
setTimeout(reject, 3000);
iframe.addEventListener('load', () => resolve(
{message: 'Probably the POST request has been sent. (This is a dummy message)'}
));
iframe.addEventListener('error', reject);
})
.then(json => {
document.body.removeChild(iframe);
document.body.removeChild(form);
return json;
})
.catch(json => {
document.body.removeChild(iframe);
document.body.removeChild(form);
throw {error: `${e ? 'Load' : 'Timeout'} Error (This is a dummy message)`};
});
}
}
function randomString()
{
return btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))));
}
function randomId()
{
return `${Math.floor(Math.random() * 1000000000)}`;
}
function buildQuery(object)
{
if (Array.isArray(object)) {
return object.map(encodeURIComponent).join('&');
}
return Object
.entries(object)
.sort((a, b) => -(a[0] < b[0]) || +(a[0] != b[0]))
.map(x => x.map(encodeURIComponent).join('='))
.join('&');
}
function buildComponents(method, url, params = {})
{
const components = Object.assign(
{
oauth_consumer_key: this.consumerKey,
oauth_signature_method: 'HMAC-SHA1',
oauth_timestamp: Math.floor(Date.now() / 1000),
oauth_version: '1.0a',
oauth_nonce: randomString(),
oauth_token: this.accessToken,
},
params
);
const body = buildQuery([method, url, buildQuery(components)]);
const key = buildQuery([this.consumerSecret, this.accessTokenSecret]);
const signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(body, key));
return Object.assign({oauth_signature: signature}, components);
}
})();