前沿:
- 插件的页面称为 popup
- 浏览器页面称为 content_scripts
- 两者桥梁称为 background
Cookie获取步骤:
- 清单配置manifest.json
-
"background": { "service_worker": "js/background.js" }, "permissions": [ "cookies", "tabs", "contextMenus", "https://*/*" ], "host_permissions": ["<all_urls>"]
-
- background.js
-
// background.js chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === 'getCookies') { chrome.cookies.getAll({ domain: message.domain }, (cookies) => { console.log(cookies, "cookie"); sendResponse({ cookies: cookies }); }); return true; //必须返回 true,以便异步处理 } });
-
- popup用到的js向background发送获取cookie请求
-
chrome.runtime.sendMessage({ type: 'getCookies', domain: domain }, function(response) { console.log(response); });
-