-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserviceworker.js
More file actions
139 lines (125 loc) · 3.64 KB
/
Copy pathserviceworker.js
File metadata and controls
139 lines (125 loc) · 3.64 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
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
// background
const isChrome = () => {
return /Chrome/.test(navigator.userAgent);
};
const app = isChrome() ? chrome : browser;
const getStorageValues = (keys = []) => {
return new Promise((resolve, reject) => {
const values = Object.create(null);
app.storage.local.get(keys, (res) => {
for (const key of Object.keys(res)) {
values[key] = res[key];
}
resolve(values);
});
});
};
app.runtime.onMessage.addListener(async function (
request,
sender,
sendResponse
) {
const cmd = request.command;
// 外部サイトで発動する機能を有効にする
if (cmd === "enable-daiiz-script") {
const funcProjectPairs = request.func_project_pairs;
const funcNames = Object.keys(funcProjectPairs);
console.log("[enable-daiiz-script]", funcNames);
for (let i = 0; i < funcNames.length; i++) {
const funcName = funcNames[i];
const projectName = funcProjectPairs[funcName];
if (!funcName || funcName.length === 0) {
return;
}
if (!projectName || projectName.length === 0) {
app.storage.local.remove([funcName], () => {
console.log("removed:", funcName);
});
} else if (projectName.length > 0) {
app.storage.local.set({ [funcName]: projectName }, () => {
console.log("set:", funcName);
});
}
}
return;
}
// 設定された値を返す
if (cmd === "get-project-name") {
console.log("[get-project-name]");
const tabs = await app.tabs.query({ currentWindow: true, active: true });
const funcNames = request.func_names;
const projectNames = await getStorageValues(funcNames);
app.tabs.sendMessage(tabs[0].id, {
command: "re:get-project-name",
projectNames,
});
return;
}
// URLのページタイトルを返却する
if (cmd === "fetch-page-title") {
const text = request.rawText;
console.log("#", "fetch-page-title", text);
await resopondWebpageTitleOrRawText(text, sendResponse);
}
});
const resopondWebpageTitleOrRawText = async (text, sendResponse) => {
if (text.match(/\n/)) {
return sendResponse(text);
}
if (text.match(/^https?:\/\/scrapbox\.io\//)) {
return sendResponse(text);
}
if (text.match(/gyazo\.com\//)) {
return sendResponse(text);
}
if (text.match(/www\.youtube\.com\//)) {
return sendResponse(text);
}
if (text.match(/www\.google/) && text.match(/\/maps\//)) {
return sendResponse(text);
}
if (text.match(/^https?:\/\//)) {
const tabs = await app.tabs.query({ currentWindow: true, active: true });
try {
await fetchPage(text, tabs);
} catch (err) {
console.error(err);
// textarea#text-inputをブロックしており、解除する必要があるので必ず応答を返す
app.tabs.sendMessage(tabs[0].id, {
command: "re:get-clipboard-page",
externalLink: text,
});
}
return;
}
return sendResponse(text);
};
const fetchPage = async (url, tabs) => {
const res = await fetch(url, { credentials: "include" });
if (!res.ok) {
throw new Error("Failed to fetch page.");
}
const body = await res.text();
// DOMParserを使えないので文字列操作でtitleを取り出す
let externalLink = url;
let title = "";
let substr = body.split("</title>")[0];
if (substr) {
substr = substr.split("<title>")[1];
}
if (substr) {
title = substr
.trim()
.split("\n")
.filter((x) => !!x.trim())
.join("");
console.log("[fetchPage]", title);
}
if (title) {
externalLink = `[${url} ${title}]`;
}
app.tabs.sendMessage(tabs[0].id, {
command: "re:get-clipboard-page",
externalLink,
});
};