-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
157 lines (127 loc) · 3.59 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Commands
chrome.commands.onCommand.addListener(function (command) {
switch (command) {
case "share2teams":
Share2Teams();
return;
case "GoodreadsEdit":
GoodreadsEdit();
return;
} // end switch
}); // end command.addListener
function CrxHelp(){
// Open CRX Help
chrome.tabs.create({ url: "https://github.com/tdalon/confluence_crx/blob/main/README.md"});
}
function CrxRn(){
// Open Crx Release Notes
chrome.tabs.create({ url:"https://github.com/tdalon/confluence_crx/blob/main/Changelog.md"});
}
// Context menus
chrome.contextMenus.removeAll();
chrome.contextMenus.create({
title: "Numbered Headings: Add Numbers",
id: "numheading_add",
contexts:["page","frame","selection","link","editable"],
"documentUrlPatterns": ["https://*.atlassian.net/wiki/*/edit-v2/*","https://*.atlassian.net/wiki/*/edit/*" ]
});
chrome.contextMenus.create({
title: "Numbered Headings: Remove Numbers",
id: "numheading_remove",
contexts:["page","frame","selection","link","editable"],
"documentUrlPatterns": ["https://*.atlassian.net/wiki/*/edit-v2/*","https://*.atlassian.net/wiki/*/edit/*" ]
});
// Top level Number limited to 6
chrome.contextMenus.create({
id: "crx_help",
title: "Help",
contexts: ["action"]
});
chrome.contextMenus.create({
id: "crx_options",
title: "Options",
contexts: ["action"]
});
chrome.contextMenus.create({
id: "crx_rn",
title: "Release Notes",
contexts: ["action"]
});
// Add Context Menu listener
chrome.contextMenus.onClicked.addListener(function(info, tab) {
switch (info.menuItemId) {
case "crx_help":
CrxHelp();
return;
case "crx_rn":
CrxRn();
return;
case "crx_options":
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
return;
case "numheading_add":
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ["jquery-3.7.1.min.js","numheading_add.js"]
});
})
return;
case "numheading_remove":
chrome.tabs.query({active: true,currentWindow: true}, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ["jquery-3.7.1.min.js","numheading_remove.js"]
});
})
return;
default:
return
} // end switch
});
// #START
chrome.omnibox.onInputEntered.addListener(
function (searchStr) {
// if user enters a keyword after the omnibox keyword, redirect search to different destination
var splitText = searchStr.split(' ');
var firstWord = splitText[0];
if (firstWord == "-h") {
CrxHelp();
return
}
if (firstWord == "-r") {
CrxRn();
return
}
chrome.tabs.update({url: chrome.runtime.getURL('popup.html?q=' + encodeURIComponent(searchStr)) + '#window'});
});
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
// ----------------------------------------------------------------------------------------------------------
function closeCurrentTab(){
// Close current tab and browser window if last tab
// chrome.tabs.remove will not close the window on last tab closure -> needs to check if single tab opened
chrome.tabs.query({
//active: true,
currentWindow: true
}, function(tabs) {
if (tabs.length > 1) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.tabs.remove(tabs[0].id, function() { });
});
} else {
chrome.windows.getCurrent(function(win) {
chrome.windows.remove(win.id, function() { });
});
}
});
} // eofun