diff --git a/chromePlugin/README.md b/chromePlugin/README.md new file mode 100644 index 0000000..e69de29 diff --git a/chromePlugin/setCookie/background.js b/chromePlugin/setCookie/background.js new file mode 100644 index 0000000..5fc12bf --- /dev/null +++ b/chromePlugin/setCookie/background.js @@ -0,0 +1,52 @@ +// background.js +console.log('✅ background.js 已加载'); +console.log('✅ chrome 对象', chrome); + +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + console.log('📬 收到消息:', request); + + if (request.action === 'injectCookies') { + const { cookies, tabId } = request; + + // 使用异步方式执行脚本 + chrome.scripting + .executeScript({ + target: { tabId: tabId }, + func: function (cookies) { + // Cookie注入函数 + const cookiePairs = cookies.split(';').map((pair) => pair.trim()); + const cookieObj = {}; + + cookiePairs.forEach((pair) => { + const [key, value] = pair.split('='); + if (key && value) { + cookieObj[key] = value; + } + }); + + // 逐个设置 Cookie + Object.keys(cookieObj).forEach((key) => { + document.cookie = `${key}=${cookieObj[key]}; path=/`; + }); + + console.log('✅ Cookie 注入完成:', cookieObj); + }, + args: [cookies], + }) + .then(() => { + // 成功执行后的响应 + sendResponse({ success: true, message: '注入成功' }); + }) + .catch((error) => { + // 错误处理 + console.error('注入失败:', error); + sendResponse({ success: false, message: error.message || '未知错误' }); + }); + + // 返回true表示异步响应 + return true; + } + + // 对于非injectCookies消息,返回false + return false; +}); diff --git a/chromePlugin/setCookie/mainifest.json b/chromePlugin/setCookie/mainifest.json new file mode 100644 index 0000000..efe3daf --- /dev/null +++ b/chromePlugin/setCookie/mainifest.json @@ -0,0 +1,15 @@ +{ + "manifest_version": 3, + "name": "Auto Cookie Injector (All Pages)", + "version": "1.4", + "description": "在任何网页上点击插件,自动注入粘贴的 Cookie 字符串", + "permissions": ["activeTab", "cookies", "scripting"], + "host_permissions": [""], + "background": { + "service_worker": "background.js" + }, + "action": { + "default_popup": "popup.html", + "default_title": "注入 Cookie" + } +} diff --git a/chromePlugin/setCookie/popup.html b/chromePlugin/setCookie/popup.html new file mode 100644 index 0000000..5ff2995 --- /dev/null +++ b/chromePlugin/setCookie/popup.html @@ -0,0 +1,42 @@ + + + + + 注入 Cookie + + + +

🔧 Cookie 注入工具

+ + + + + + diff --git a/chromePlugin/setCookie/popup.js b/chromePlugin/setCookie/popup.js new file mode 100644 index 0000000..4d1d457 --- /dev/null +++ b/chromePlugin/setCookie/popup.js @@ -0,0 +1,52 @@ +// popup.js +document.addEventListener('DOMContentLoaded', () => { + const pasteBtn = document.getElementById('paste'); + const injectBtn = document.getElementById('inject'); + const cookieInput = document.getElementById('cookie'); + + pasteBtn.addEventListener('click', async () => { + try { + const clipboardText = await navigator.clipboard.readText(); + cookieInput.value = clipboardText; + } catch (err) { + alert('❌ 无法读取剪贴板:' + err.message); + } + }); + + injectBtn.addEventListener('click', async () => { + const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); + const cookies = cookieInput.value.trim(); + + if (!cookies) { + alert('请输入 Cookie 内容'); + return; + } + + try { + // ✅ 通过 message 发送给 background.js + // 使用 Promise 方式处理响应 + const response = await new Promise((resolve) => { + chrome.runtime.sendMessage( + { + action: 'injectCookies', + cookies: cookies, + tabId: tab.id, + }, + resolve + ); + }); + + // 检查响应是否存在 + if (response && response.success) { + alert('✅ Cookie 注入成功!'); + } else if (response && response.message) { + alert('❌ 注入失败:' + response.message); + } else { + alert('❌ 注入失败:未知错误'); + } + } catch (error) { + console.error('发送消息失败:', error); + alert('❌ 消息发送失败:' + error.message); + } + }); +});