-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
97 lines (93 loc) · 2.54 KB
/
content.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
LSEXT = {
setToken: function(token, callback) {
callback = callback ? callback : function() {};
chrome.storage.local.set({ 'token': token }, function() {
callback();
});
},
getToken: function(callback) {
callback = callback ? callback : function() {};
chrome.storage.local.get({ 'token': '' }, function(items) {
callback(items.token);
});
}
};
jQuery(document).ready(function($) {
var fingerprint = new Fingerprint().get();
var loading = false;
var loginTriggerText = 'Login with this browser';
var logoutTriggerText = 'Log this browser out';
var loginTrigger = $('<a href="#" class="btn btn-default">'+loginTriggerText+'</a>');
var logoutTrigger = $('<a href="#" class="btn btn-default">'+logoutTriggerText+'</a>');
var form = $('form.form-login');
var formLogin = form.find('.btn-primary');
var loginError = $('#login-error');
var next = form.find('input[name="next"]').val();
if (!next) { next = '/'; }
LSEXT.getToken(function(token) {
if (token) {
// show logout button
formLogin.after(logoutTrigger);
bindLogoutAction();
} else {
// show login button
formLogin.after(loginTrigger);
bindLoginAction();
}
});
function bindLogoutAction() {
logoutTrigger.on('click', function(e) {
e.preventDefault();
if (!loading) {
loading = true;
logoutTrigger.html('<span class="loader"></span>');
LSEXT.setToken('', function() {
logoutTrigger.html('Logged out!');
setTimeout(function() {
loading = false;
logoutTrigger.after(loginTrigger);
bindLoginAction();
logoutTrigger.remove();
}, 1000);
});
}
});
}
function bindLoginAction() {
loginTrigger.on('click', function(e) {
e.preventDefault();
if (!loading) {
loading = true;
loginTrigger.html('<span class="loader"></span>');
var postData = {
username: form.find('input[name="username"]').val(),
password: form.find('input[name="password"]').val(),
device_name: 'Browser',
device_type: 'browser',
device_id: fingerprint
};
$.ajax({
type: "POST",
url: '/api/v2/devices/',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(postData),
success: function(resp) {
LSEXT.setToken(resp.token, function() {
if (resp.token) {
window.location = next;
} else {
loginTrigger.html('Sorry, something went wrong.');
}
});
},
error: function(e) {
loading = false;
loginTrigger.html(loginTriggerText);
loginError.show();
}
});
}
});
}
});