-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
129 lines (117 loc) · 3.52 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
addChangeContextMenuItems();
chrome.browserAction.onClicked.addListener(tab => {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "updateBadgeText" }, function(
response
) {
// for now, nothing to do here. just sending message and return will
// come frm a sendMessage in inject-script.js
});
});
});
chrome.runtime.onMessage.addListener(function(request, sender) {
const friendlySizeBytesFour = bytes => {
if (!bytes) {
return "---";
}
if (bytes == 0) {
return "0.0 B";
} else if (bytes < 1000) {
// returns 7b
return `${bytes}b`;
} else if (bytes < 1000000) {
// returns 70K
return `${(bytes / 1000).toFixed(0)}K`;
} else if (bytes < 10000000) {
// returns 2.7M
return `${(bytes / 1000000).toFixed(1)}M`;
} else if (bytes < 1000000000) {
// returns 27M
return `${(bytes / 1000000).toFixed(0)}M`;
} else {
// returns >1G
return ">1G";
}
};
const setBadgeTextFunction = () => {
const badgeText =
request && request.nextJsDataLength && request.nextJsDataLength > 10
? friendlySizeBytesFour(request.nextJsDataLength)
: "n/a";
nextDataFullValue =
request && request.nextJsDataLength && request.nextJsDataLength > 10
? request.nextJsData
: "{}";
chrome.browserAction.setBadgeText({
text: badgeText,
tabId: sender.tab.id
});
};
if (request.messageType === "BADGE_TEXT") {
setBadgeTextFunction();
}
if (request.messageType === "SHOW_DATA") {
// always set badge Text first
setBadgeTextFunction();
window.localStorage.setItem("nextdata", request.nextJsData);
chrome.tabs.create(
{ url: chrome.extension.getURL("viewNextData.html") },
function(tab) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, { greeting: "hello" }, function(
response
) {
console.log(response.farewell);
});
});
}
);
}
});
// bug fix for dec tools problem below
// https://stackoverflow.com/questions/28786723/why-doesnt-chrome-tabs-query-return-the-tabs-url-when-called-using-requirejs
let activeTabId;
chrome.tabs.onActivated.addListener(function(activeInfo) {
activeTabId = activeInfo.tabId;
});
function getActiveTab(callback) {
chrome.tabs.query({ currentWindow: true, active: true }, function(tabs) {
var tab = tabs[0];
if (tab) {
callback(tab);
} else {
chrome.tabs.get(activeTabId, function(tab) {
if (tab) {
callback(tab);
} else {
console.log("No active tab identified.");
}
});
}
});
}
function addChangeContextMenuItems() {
// remove past menu items first
if (chrome.contextMenus && chrome.contextMenus.removeAll) {
chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
title: "View NextJS __NEXT_DATA__",
contexts: ["browser_action"],
onclick: function() {
chrome.tabs.query({ active: true, currentWindow: true, }, function(
tabs
) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "showNextJsData" },
function(response) {
// for now, nothing to do here. just sending message and return will
// come frm a sendMessage in inject-script.js
}
);
});
}
});
});
}
}