2024-11-10 00:24:12 +00:00
|
|
|
class MessagingService {
|
|
|
|
|
2024-11-17 00:19:48 +00:00
|
|
|
async GetHyperlinkNodes(sender, callback) {
|
2024-11-10 00:24:12 +00:00
|
|
|
const a = this;
|
2024-12-15 18:40:40 +00:00
|
|
|
const nodes = await document.querySelectorAll("a");
|
|
|
|
|
2024-11-10 00:24:12 +00:00
|
|
|
let result = [];
|
|
|
|
|
|
|
|
for (let i=0; i<nodes.length; i++) {
|
|
|
|
result.push({
|
|
|
|
innerText: nodes[i].innerText ?? "",
|
2024-11-17 00:19:48 +00:00
|
|
|
innerHTML: a.#encodeHtml(nodes[i].innerHTML),
|
2024-11-10 00:24:12 +00:00
|
|
|
href: (nodes[i].getAttribute("href") ?? "").trim(),
|
|
|
|
title: (nodes[i].getAttribute("title") ?? "").trim(),
|
|
|
|
target: nodes[i].getAttribute("target") ?? "",
|
|
|
|
rel: nodes[i].getAttribute("rel") ?? "",
|
|
|
|
type: nodes[i].getAttribute("type") ?? "",
|
|
|
|
occurrence: 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await callback(result);
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:19:48 +00:00
|
|
|
async GetImageNodes(sender, callback) {
|
|
|
|
const a = this;
|
2024-12-15 18:40:40 +00:00
|
|
|
const nodes = await document.querySelectorAll("img");
|
|
|
|
|
2024-11-17 00:19:48 +00:00
|
|
|
let result = [];
|
|
|
|
|
|
|
|
for (let i=0; i<nodes.length; i++) {
|
|
|
|
result.push({
|
|
|
|
src: (nodes[i].getAttribute("src") ?? ""),
|
|
|
|
alt: (nodes[i].getAttribute("alt") ?? ""),
|
|
|
|
srcset: nodes[i].getAttribute("srcset") ?? "",
|
|
|
|
usemap: nodes[i].getAttribute("usemap") ?? "",
|
|
|
|
width: nodes[i].getAttribute("width") ?? "",
|
|
|
|
height: nodes[i].getAttribute("height") ?? "",
|
|
|
|
occurrence: 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await callback(result);
|
|
|
|
}
|
|
|
|
|
2024-11-10 00:24:12 +00:00
|
|
|
async GetLocation(sender, callback) {
|
|
|
|
callback(document.location.href);
|
|
|
|
}
|
|
|
|
|
2024-12-15 18:40:40 +00:00
|
|
|
async GetMetaNodes(sender, callback) {
|
|
|
|
const a = this;
|
|
|
|
const nodes = await document.querySelectorAll("meta");
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
for (let i=0; i<nodes.length; i++) {
|
|
|
|
result.push({
|
|
|
|
content: (nodes[i].getAttribute("content") ?? ""),
|
|
|
|
name: (nodes[i].getAttribute("name") ?? ""),
|
|
|
|
httpEquiv: (nodes[i].getAttribute("http-equiv") ?? ""),
|
|
|
|
occurrence: 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await callback(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
async GetScriptNodes(sender, callback) {
|
|
|
|
const a = this;
|
|
|
|
const nodes = await document.querySelectorAll("script");
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
for (let i=0; i<nodes.length; i++) {
|
|
|
|
result.push({
|
|
|
|
src: (nodes[i].getAttribute("src") ?? ""),
|
|
|
|
type: (nodes[i].getAttribute("type") ?? ""),
|
|
|
|
occurrence: 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await callback(result);
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:19:48 +00:00
|
|
|
#encodeHtml(htmlContent) {
|
|
|
|
const map = {
|
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
'"': """,
|
|
|
|
"'": "'",
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = (htmlContent ?? "").replace("\r", "").replace("\n", "\\n").replace("\t", "\\t");
|
|
|
|
result = result.replace(/[&<>"']/g, (char) => map[char]);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-11-10 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const messageService = new MessagingService();
|
|
|
|
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(async function(request, sender, callback) {
|
|
|
|
|
|
|
|
if (typeof(messageService[request.event]) == "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await messageService[request.event](sender, callback);
|
|
|
|
|
2024-12-15 18:40:40 +00:00
|
|
|
});
|