-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
59 lines (49 loc) · 1.85 KB
/
background.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
const regex_host = /^(http[s]?:\/)?\/?([^:\/\s]+)(:\d*)?/g;
const regex_params = /(\w*=\w*)/g;
const editor_value = "/editor.html";
const wcmmode_value = "wcmmode=disabled";
const crx_value = "/crx/de/index.jsp";
const system_value = "/system/console/bundles";
chrome.commands.onCommand.addListener(function (command) {
chrome.tabs.getSelected(null, function (tab) {
let newUrl = "";
const host = tab.url.match(regex_host) + "";
if (host !== null)
switch (command) {
case "open-crx":
newUrl = host + crx_value;
break;
case "open-console":
newUrl = host + system_value;
break;
case "toggle-editor-preview":
newUrl = toggleEditorPreview(tab.url, host);
break;
case "current-path-crx":
newUrl = openCurrentPathCrx(tab.url, host);
break;
}
newUrl !== "" && chrome.tabs.create({url: newUrl, index: tab.index + 1});
});
});
toggleEditorPreview = (url, host) => {
let params = url.match(regex_params);
url = url.replace(/\?.*/, "");
if (params === null)
params = [];
else if (params.includes(wcmmode_value))
params.splice(params.indexOf(wcmmode_value), 1);
if (url.includes(editor_value)) {
url = url.replace(editor_value, "");
params.unshift(wcmmode_value);
} else {
url = url.slice(0, host.length) + editor_value + url.slice(host.length);
}
return params.length <= 0 ? url : url + "?" + params.join("&");
};
openCurrentPathCrx = (url, host) => {
url = url.replace(editor_value, "")
.replace(/\?.*/, "")
.replace(".html", "");
return url.slice(0, host.length) + crx_value + "#" + url.slice(host.length);
};