-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
97 lines (79 loc) · 2.76 KB
/
background.js
File metadata and controls
97 lines (79 loc) · 2.76 KB
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
const tabState = {};
const HVI_MENU_ID = "HelpViewerCtxHelpGet";
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: HVI_MENU_ID,
title: "❔",
contexts: ["all"]
//, documentUrlPatterns: ["<all_urls>"]
}, () => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
return;
}
chrome.contextMenus.update(HVI_MENU_ID, { visible: false });
});
});
function fetchHelpFileWithRetry(tabId, attempt = 0) {
chrome.scripting.executeScript(
{
target: { tabId },
world: "MAIN",
func: () => window.hvHelpFile || null
},
(results) => {
if (chrome.runtime.lastError) {
console.warn("executeScript error:", chrome.runtime.lastError.message);
return;
}
const helpFile = results?.[0]?.result;
console.log("helpFile - try : ", attempt, " :", helpFile);
if (!helpFile && attempt < 10) {
setTimeout(() => fetchHelpFileWithRetry(tabId, attempt + 1), 300);
return;
}
if (!tabState[tabId]) tabState[tabId] = {};
tabState[tabId].helpFile = helpFile || null;
chrome.contextMenus.update(HVI_MENU_ID, { visible: !!helpFile });
}
);
}
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg.type === "requestHelpFile" && sender.tab) {
fetchHelpFileWithRetry(sender.tab.id);
}
});
chrome.tabs.onActivated.addListener((activeInfo) => {
const tabId = activeInfo.tabId;
chrome.tabs.get(tabId, (tab) => {
if (chrome.runtime.lastError || !tab || !tab.id) return;
fetchHelpFileWithRetry(tab.id);
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId !== HVI_MENU_ID) return;
if (!tab || !tab.id) return;
const state = tabState[tab.id] || {};
const helpFile = state.helpFile;
if (!helpFile) return;
chrome.tabs.sendMessage(tab.id, { type: "getElementInfo" }, (elemInfo) => {
if (chrome.runtime.lastError || !elemInfo) {
console.warn("Not any element info found:", chrome.runtime.lastError?.message);
return;
}
let elementId = elemInfo.id?.replace(/\|/g, '-') || '';
let urlSplits = '';
if (helpFile.routing) {
urlSplits = (tab.url ? tab.url.split('?')[0]?.split('/') : []).filter(x => x).slice(1 + (helpFile.offset || 0));
if (elementId)
elementId = `-${elementId}`;
elementId = urlSplits.join('-')?.replace(/\:|\./g, '-') + elementId;
}
const baseUrl = helpFile.viewer || 'https://helpviewer.github.io/index.html';
const filePath = helpFile.file || '';
const targetUrl = baseUrl +
"?d=" + encodeURIComponent(filePath) +
"&p=" + encodeURIComponent(String(elementId || 'noid')) + (helpFile.extension || ".md");
chrome.tabs.create({ url: targetUrl });
});
});