我是網絡編程的新手,但不是一般的編程。我正在嘗試構建一個 Google Chrome 擴展程序,每當用戶訪問 StackOverflow.com 上的任何頁面時,它都會自動保存 URL。然后我想在擴展彈出窗口中顯示 URL 列表。到目前為止,我已經決定我需要一個 Page Action 擴展,因為我只希望它在 StackOverflow 頁面上處于活動狀態。我了解訪問選項卡及其 URL 的不同方式,但我不知道將代碼放在哪里。例如,我不確定是否需要內容腳本和/或背景腳本。我也很困惑 Listeners 以及我是否需要在內容腳本和事件頁面或彈出腳本之間發送消息。這是我到目前為止所擁有的:清單.json "manifest_version": 2, "name": "Stack Overflow visit history", "version": "1.0", "description": "This Chrome extension stores your Stack Overflow URL visits.", "icons": { "128": "icon128.png", "48": "icon48.png", "16": "icon16.png" }, "page_action": { "default_icon": "icon16.png", "default_popup": "popup.html", "default_title": "StackOverflowVisits" }, "background": { "scripts": ["eventPage.js"], "persistent": false }, "content_scripts": [ { "matches": ["https://stackoverflow.com/*"], "js": ["content.js", "jquery-3.5.1.min.js"] } ], "permissions": [ "webNavigation", "tabs", "https://stackoverflow.com/*" ]}popup.html<html> <head> <title>Stack Overflow Visit History</title> <style> body {min-width: 250px;} </style> <script src="jquery-3.5.1.min.js"></script> <script src="popup.js"></script> </head> <body> <h2>StackOverflowVisits</h2> <div id="newUrlList"></div> </body></html>popup.js var tabUrl = tabs[0].url; chrome.storage.local.get({'urlList':[]}, function(item){ var newUrlList = item.urlList; if (newUrlList.indexOf(tabUrl) === -1) { newUrlList.push(tabUrl); } console.log(newUrlList); chrome.storage.local.set({urlList: newUrlList}); });});內容.jschrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { chrome.extension.getBackgroundPage().console.log(tab.url); chrome.storage.sync.set({tabId : tab.url})});
當用戶訪問特定站點時,如何在 Google Chrome 擴展程序中自動保存 URL?
一只斗牛犬
2022-10-08 17:29:56