-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.js
67 lines (60 loc) · 1.83 KB
/
scripts.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
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function setup() {
if (!NewDB("user_mention_service"))
print("Create database for user_mention_service failed.");
if (!NewDB("url_service"))
print("Create database for url_service failed.");
}
function user_register(userid, username) {
DBSet("user_mention_service", username, str2ab(userid));
}
function user_mention_service(mentions) {
let length = mentions.length;
let results = [];
for (let i = 0; i < length; i++) {
let username = mentions[i].substring(1);
let userid = ab2str(DBGet("user_mention_service", username));
results.push([username, userid]);
}
print(results);
return;
}
function gen_random_str(length) {
let char_map = "abcdefghijklmnopqrstuvwxyzABCDEF"
"GHIJKLMNOPQRSTUVWXYZ0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result = result + char_map[Math.floor(Math.random() * (char_map.length))];
}
return result;
}
function url_service(urls) {
let length = urls.length;
let results = [];
for (let i = 0; i < length; i++) {
let shorturl = "http://short-url/" + gen_random_str(10);
results.push([urls[i], shorturl]);
}
print(results);
return;
}
function test(val) {
print(val);
let user_mentions = val.match(/@[a-zA-Z0-9-_]+/g);
// print(user_mentions);
let urls = val.match(/(http:\/\/|https:\/\/)([a-zA-Z0-9_!~*'().&=+$%-]+)/g);
// print(urls);
Call("user_mention_service", user_mentions);
Call("url_service", urls);
return "OK\n";
}