-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
104 lines (82 loc) · 2.71 KB
/
popup.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
"use strict";
window.TikAPI = new function(){
const config = {
OAUTH_URL: 'https://tikapi.io/account/authorize',
available_scopes: ["CONVERSATION_REQUESTS","EDIT_PROFILE","EXPLORE","FOLLOW_ACTIONS","LIVE","MEDIA_ACTIONS","SEND_MESSAGES","VIEW_ANALYTICS","VIEW_COINS","VIEW_COLLECTIONS","VIEW_FOLLOWERS","VIEW_MESSAGES","VIEW_NOTIFICATIONS","VIEW_PROFILE"]
};
this.callback = null;
this.popup = function(options){
if(!options){
throw new Error("The client id is required.");
}
options = (options.constructor.name === "Object" || !(arguments.length > 1)) ? {
client_id: options.client_id,
scope: options.scope || options.scopes,
state: options.state,
email: options.email,
country: options.country,
url: options.url
} : {
client_id: arguments[0],
scope: arguments[1],
state: arguments[2],
email: arguments[3],
country: arguments[4],
url: arguments[5]
};
options.client_id = function(client){
if(!/^c_[a-zA-Z0-9]{10,}$/.test(client)){
throw new Error("The client id seems invalid.");
}
return client;
}(options.client_id);
options.scope = function(scopes){
if(!scopes || scopes == ""){
return false;
}
scopes = (typeof scopes === "string") ? decodeURIComponent(scopes).split(" ") : scopes;
if(!scopes || !Array.isArray(scopes) || scopes.length == 0){
alert("A valid scope array is required.")
throw new Error("Invalid scopes");
}
var validScopes = scopes.map(function(s){
if(!s || typeof s !== "string" || !config.available_scopes.includes(s.trim().toUpperCase())){ return false}
return s.trim().toUpperCase();
}).filter(function(s){
return s;
});
if(validScopes.length !== scopes.length){
alert("Some scopes are invalid.")
throw new Error("Some scopes are invalid");
}
return validScopes.join('+');
}(options.scope);
options.is_popup = true;
var urlQuery = new URLSearchParams();
for(var option in options){
if(!options[option]){
continue;
}
urlQuery.append(option,options[option]);
}
var url = `${options.url || config.OAUTH_URL}?${urlQuery.toString()}`;
var left = (window.screen.width/2)-(500/2);
var top = (window.screen.height/2)-(500/2);
var child = window.open(url,"Login with TikTok",`toolbar=no, width=500, height=720, top=${top}, left=${left}`);
return url;
}
//alias
this.oauth = this.popup;
//Oauth Event Function Setter
this.onLogin = function(callback){
this.callback = callback;
}
//Oauth Event Handler
window.addEventListener("message",(function(event){
if(!event.data || !event.data._tikapi) return;
if(typeof this.callback === "function"){
return this.callback(event.data);
}
}).bind(this), false);
return this;
}();