-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
46 lines (40 loc) · 1.32 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
var fmtJsonContextMenuItem = {
"id": "Jipy:fmtJSON"
, "title": "Format JSON"
, "contexts": ["selection"]
};
var fmtJsonStringContextMenuItem = {
"id": "Jipy:fmtJSONString"
, "title": "Format Escaped JSON"
, "contexts": ["selection"]
};
function runFormatter(cmd) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, cmd);
}
});
}
// SEE: https://developer.chrome.com/extensions/background_pages#initialization
chrome.runtime.onInstalled.addListener(function () {
chrome.contextMenus.create(fmtJsonContextMenuItem);
chrome.contextMenus.create(fmtJsonStringContextMenuItem);
});
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.sendMessage(tab.id, fmtJsonContextMenuItem.id);
});
chrome.contextMenus.onClicked.addListener(function (data) {
if (data.menuItemId !== fmtJsonContextMenuItem.id
&& data.menuItemId !== fmtJsonStringContextMenuItem.id) {
return;
}
runFormatter(data.menuItemId);
});
chrome.commands.onCommand.addListener(function (cmd) {
if (cmd === 'FormatJSON') {
runFormatter(fmtJsonContextMenuItem.id);
}
else if (cmd === 'FormatJSONString') {
runFormatter(fmtJsonStringContextMenuItem.id);
}
});