add first commit
This commit is contained in:
commit
421b986585
|
|
@ -0,0 +1,11 @@
|
||||||
|
chrome.runtime.onInstalled.addListener(() => {
|
||||||
|
chrome.storage.local.get("replacements", (data) => {
|
||||||
|
if (!data.replacements) {
|
||||||
|
fetch(chrome.runtime.getURL("replacements.json"))
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((json) => {
|
||||||
|
chrome.storage.local.set({ replacements: json });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
// 替換文字的函數
|
||||||
|
function replaceText(node, replacements) {
|
||||||
|
if (node.nodeType === Node.TEXT_NODE) {
|
||||||
|
let text = node.nodeValue;
|
||||||
|
for (let [key, value] of Object.entries(replacements)) {
|
||||||
|
const regex = new RegExp(key, "g");
|
||||||
|
text = text.replace(regex, value);
|
||||||
|
}
|
||||||
|
node.nodeValue = text;
|
||||||
|
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
||||||
|
node.childNodes.forEach((child) => replaceText(child, replacements));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化替換
|
||||||
|
function initializeReplacements() {
|
||||||
|
chrome.storage.local.get("replacements", (data) => {
|
||||||
|
const replacements = data.replacements || {};
|
||||||
|
replaceText(document.body, replacements);
|
||||||
|
|
||||||
|
// 監控動態內容
|
||||||
|
const observer = new MutationObserver((mutations) => {
|
||||||
|
mutations.forEach((mutation) => {
|
||||||
|
mutation.addedNodes.forEach((node) => {
|
||||||
|
if (node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.TEXT_NODE) {
|
||||||
|
replaceText(node, replacements);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
observer.observe(document.body, { childList: true, subtree: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeReplacements();
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
{
|
||||||
|
"manifest_version": 3,
|
||||||
|
"name": "支語寶",
|
||||||
|
"version": "0.1.0 alpha",
|
||||||
|
"description": "將所有網頁中的指定詞語替換為其他詞語。",
|
||||||
|
"permissions": ["storage", "activeTab", "scripting"],
|
||||||
|
"host_permissions": ["<all_urls>"],
|
||||||
|
"background": {
|
||||||
|
"service_worker": "background.js"
|
||||||
|
},
|
||||||
|
"options_page": "options.html",
|
||||||
|
"content_scripts": [
|
||||||
|
{
|
||||||
|
"matches": ["<all_urls>"],
|
||||||
|
"js": ["content.js"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>支語寶</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; padding: 20px; }
|
||||||
|
table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
|
||||||
|
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
|
||||||
|
th { background-color: #f4f4f4; }
|
||||||
|
button { margin-right: 10px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>支語寶</h1>
|
||||||
|
<table id="replacementsTable">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>原始詞語</th>
|
||||||
|
<th>替換詞語</th>
|
||||||
|
<th>操作</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody></tbody>
|
||||||
|
</table>
|
||||||
|
<input type="text" id="original" placeholder="原始詞語">
|
||||||
|
<input type="text" id="replacement" placeholder="替換詞語">
|
||||||
|
<button id="addButton">新增</button>
|
||||||
|
|
||||||
|
<script src="options.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const tableBody = document.querySelector("#replacementsTable tbody");
|
||||||
|
const originalInput = document.getElementById("original");
|
||||||
|
const replacementInput = document.getElementById("replacement");
|
||||||
|
const addButton = document.getElementById("addButton");
|
||||||
|
|
||||||
|
// 載入詞語對
|
||||||
|
function loadReplacements() {
|
||||||
|
chrome.storage.local.get("replacements", (data) => {
|
||||||
|
const replacements = data.replacements || {};
|
||||||
|
tableBody.innerHTML = "";
|
||||||
|
for (const [key, value] of Object.entries(replacements)) {
|
||||||
|
addRow(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增表格行
|
||||||
|
function addRow(original, replacement) {
|
||||||
|
const row = document.createElement("tr");
|
||||||
|
row.innerHTML = `
|
||||||
|
<td>${original}</td>
|
||||||
|
<td>${replacement}</td>
|
||||||
|
<td>
|
||||||
|
<button class="deleteButton">刪除</button>
|
||||||
|
</td>
|
||||||
|
`;
|
||||||
|
row.querySelector(".deleteButton").addEventListener("click", () => {
|
||||||
|
deleteReplacement(original);
|
||||||
|
});
|
||||||
|
tableBody.appendChild(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增詞語對
|
||||||
|
addButton.addEventListener("click", () => {
|
||||||
|
const original = originalInput.value.trim();
|
||||||
|
const replacement = replacementInput.value.trim();
|
||||||
|
if (!original || !replacement) return alert("請輸入有效的詞語!");
|
||||||
|
chrome.storage.local.get("replacements", (data) => {
|
||||||
|
const replacements = data.replacements || {};
|
||||||
|
replacements[original] = replacement;
|
||||||
|
chrome.storage.local.set({ replacements }, loadReplacements);
|
||||||
|
});
|
||||||
|
originalInput.value = "";
|
||||||
|
replacementInput.value = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
// 刪除詞語對
|
||||||
|
function deleteReplacement(original) {
|
||||||
|
chrome.storage.local.get("replacements", (data) => {
|
||||||
|
const replacements = data.replacements || {};
|
||||||
|
delete replacements[original];
|
||||||
|
chrome.storage.local.set({ replacements }, loadReplacements);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
loadReplacements();
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"打印": "列印",
|
||||||
|
"搜索": "搜尋"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue