commit 421b9865855a882875cc6499e59d92c608942bb5 Author: tangsong Date: Thu Jan 16 17:35:02 2025 +0800 add first commit diff --git a/background.js b/background.js new file mode 100644 index 0000000..357f3e3 --- /dev/null +++ b/background.js @@ -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 }); + }); + } + }); + }); \ No newline at end of file diff --git a/content.js b/content.js new file mode 100644 index 0000000..b21f6ad --- /dev/null +++ b/content.js @@ -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(); \ No newline at end of file diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..c7da974 --- /dev/null +++ b/manifest.json @@ -0,0 +1,18 @@ +{ + "manifest_version": 3, + "name": "支語寶", + "version": "0.1.0 alpha", + "description": "將所有網頁中的指定詞語替換為其他詞語。", + "permissions": ["storage", "activeTab", "scripting"], + "host_permissions": [""], + "background": { + "service_worker": "background.js" + }, + "options_page": "options.html", + "content_scripts": [ + { + "matches": [""], + "js": ["content.js"] + } + ] + } \ No newline at end of file diff --git a/options.html b/options.html new file mode 100644 index 0000000..77b5791 --- /dev/null +++ b/options.html @@ -0,0 +1,33 @@ + + + + + + 支語寶 + + + +

支語寶

+ + + + + + + + + +
原始詞語替換詞語操作
+ + + + + + + \ No newline at end of file diff --git a/options.js b/options.js new file mode 100644 index 0000000..b6bff15 --- /dev/null +++ b/options.js @@ -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 = ` + ${original} + ${replacement} + + + + `; + 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(); + }); \ No newline at end of file diff --git a/replacements.json b/replacements.json new file mode 100644 index 0000000..5ddb4cf --- /dev/null +++ b/replacements.json @@ -0,0 +1,4 @@ +{ + "打印": "列印", + "搜索": "搜尋" + } \ No newline at end of file