forked from nareix/majia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
124 lines (102 loc) · 3.02 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
var reloadCurrentTab = function () {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.reload(tabs[0].id);
});
};
var reloadPopup = function () {
window.location.reload();
};
var callApi = function (op, params) {
return new Promise(function (fulfill, reject) {
params = params || {};
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
var matches = urlParseRE.exec(tabs[0].url);
params.host = matches[11];
chrome.runtime.sendMessage(null, {op: op, params: params}, {}, fulfill);
});
});
};
var selectProfile = function (id) {
callApi('selectProfile', {id: id}).then(function () {
reloadCurrentTab();
reloadPopup();
});
};
var newProfile = function () {
callApi('newProfile').then(function () {
reloadCurrentTab();
reloadPopup();
});
};
var deleteCurrentProfile = function () {
callApi('deleteCurrentProfile').then(function () {
reloadCurrentTab();
reloadPopup();
});
};
var updateProfile = function (id, set) {
callApi('updateProfile', {id: id, $set: set}).then(function () {
reloadPopup();
});
};
document.addEventListener('DOMContentLoaded', function () {
callApi('getProfiles').then(function (data) {
var profiles = [];
for (var id in data.profiles) {
var profile = data.profiles[id];
profile.id = id;
profiles.push(profile);
}
profiles.sort(function (a, b) {
return a.id - b.id;
})
var mainDiv = document.createElement('div');
profiles.forEach(function (profile) {
var title = profile.title;
if (profile.id == data.currentProfileId)
title = '['+title+']';
var button = document.createElement('button');
button.innerHTML = title;
if (profile.id != data.currentProfileId) {
button.onclick = function () {
selectProfile(profile.id);
};
} else {
button.style.color = 'red';
}
mainDiv.appendChild(button);
});
var hr = document.createElement('p');
mainDiv.appendChild(hr);
var button = document.createElement('button');
button.innerHTML = '新建..';
button.onclick = newProfile;
mainDiv.appendChild(button);
if (profiles.length > 1) {
var button = document.createElement('button');
button.innerHTML = '删除';
button.onclick = deleteCurrentProfile;
mainDiv.appendChild(button);
var button = document.createElement('button');
var input = document.createElement('input');
input.value = data.profiles[data.currentProfileId].title;
button.innerHTML = '重命名';
button.onclick = function () {
button.parentNode.replaceChild(input, button);
input.focus();
input.onkeypress = function (e) {
if (e.keyCode == 13) {
updateProfile(data.currentProfileId, {
title: input.value,
});
return false;
}
return true;
};
};
mainDiv.appendChild(button);
}
document.body.appendChild(mainDiv);
});
});