-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
133 lines (104 loc) · 2.94 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
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
const port = chrome.extension.connect({
name: "Sample Communication"
});
$(document).ready(function () {
$("#tocken").hide();
const url = localStorage.getItem("url");
if (url) $("#authUrl").val(url);
const clientId = localStorage.getItem("clientId");
if (clientId) $("#client").val(clientId);
const username = localStorage.getItem("username");
if (username) $("#username").val(username);
const password = localStorage.getItem("password");
if (password) $("#password").val(password);
const filterUrls = localStorage.getItem("filterUrls");
if (filterUrls || localStorage.getItem("emptyUrls") === "true") $("#filterUrls").val(filterUrls);
$('form').submit(function (e) {
e.preventDefault();
});
$('#login').click(() => login());
$('#copy').click(() => login(true));
$('#onlyCopy').click(() => login(true, "", true));
$('#copyWithBearer').click(() => login(true, "Bearer "));
$('#onlyCopyWithBearer').click(() => login(true, "Bearer ", true));
$('#disable').click(function () {
port.postMessage({
type: 'DISABLE'
});
});
});
function parseUrls(urlsStr) {
return urlsStr
.split(",")
.map(c => c.trim())
.filter(c => c)
.map(c => "*://" + c + "/*");
}
function copy() {
var copyText = document.getElementById("tocken");
copyText.select();
document.execCommand("Copy");
}
function login(wantCopy, copyPrefix, nonAutoHeader) {
const url = $("#authUrl").val();
const clientId = $("#client").val();
const username = $("#username").val();
const password = $("#password").val();
const filterUrls = $("#filterUrls").val();
const urls = parseUrls(filterUrls);
localStorage.setItem("url", url);
localStorage.setItem("clientId", clientId);
localStorage.setItem("username", username);
localStorage.setItem("password", password);
localStorage.setItem("filterUrls", filterUrls);
if (urls && urls.length) {
localStorage.removeItem("emptyUrls");
} else {
localStorage.setItem("emptyUrls", "true")
}
$.ajax({
url: url,
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: {
"grant_type": "password",
"client_id": clientId,
"username": username,
"password": password
},
//authUrl
complete: function () {
//called when complete
},
success: function (response) {
if (wantCopy) {
$("#tocken").val((copyPrefix ? copyPrefix : "") + response.access_token);
$("#tocken").show();
copy();
$("#tocken").hide();
}
if (!nonAutoHeader) {
port.postMessage({
type: "ENABLE",
accessToken: response.access_token,
urls: urls
});
}
$.notify({
message: 'Success'
}, {
type: 'success'
});
setTimeout(function () {
window.close();
}, 500);
},
error: function () {
$.notify({
message: 'Fail'
}, {
type: 'danger'
});
},
});
};