-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
96 lines (81 loc) · 2.69 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
const getTextNodes = () => {
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT
);
let node;
const textNodes = [];
while ((node = walker.nextNode())) {
const style = getComputedStyle(node.parentElement);
textNodes.push([style.fontFamily, style.fontStyle, node.nodeValue]);
}
return textNodes;
};
const unicode = (characters) => {
const groups = characters
.map((c) => c.charCodeAt())
.reduce((groups, code, index) => {
const key = code - index;
if (!groups[key]) groups[key] = [];
groups[key].push(code.toString(16));
return groups;
}, {});
return Object.keys(groups)
.map((key) => {
const values = groups[key];
if (values.length == 1) {
return `U+${values[0]}`.toUpperCase();
}
return `U+${values[0]}-${values.pop()}`.toUpperCase();
})
.join(",");
};
const parse = (textNodes) => {
if (!textNodes) return null;
const getName = (name) => name.split(",")[0].replaceAll('"', "");
const getKey = (item) => `${getName(item[0])}-${item[1]}`;
const groups = textNodes.reduce((groups, current) => {
const key = getKey(current);
if (!groups[key]) groups[key] = [];
groups[key].push(current);
return groups;
}, {});
return Object.keys(groups).map((key) => {
const [name, style] = key.split("-");
const characters = Array.from(
new Set(groups[key].map((item) => item[2]).join(""))
).sort();
return {
"font-family": name,
"font-style": style,
characters: characters.join(""),
"unicode-range": unicode(characters),
};
});
};
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: false })
.catch((error) => console.error(error));
chrome.action.onClicked.addListener((tab) => {
if (tab.url?.startsWith("chrome://")) return;
chrome.sidePanel.open({ tabId: tab.id });
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"],
});
});
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await chrome.tabs.get(activeInfo.tabId);
if(!tab.url) return
if (tab.url?.startsWith("chrome://")) return;
chrome.scripting.executeScript({
target: { tabId: activeInfo.tabId },
files: ["content.js"],
// func: getTextNodes
});
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
chrome.storage.session.set({
unicode: JSON.stringify(parse(message.textNodes), null, 2),
});
});