commit a356577360bf0073bf267df1887e671d949d6455 Author: 小陌 <159088129@qq.com> Date: Fri Apr 7 14:19:41 2023 +0800 创建 diff --git a/content.js b/content.js new file mode 100644 index 0000000..8940e75 --- /dev/null +++ b/content.js @@ -0,0 +1,30 @@ +(function() { + document.addEventListener('DOMContentLoaded', function() { + var div = document.createElement('div') + div.id = 'cookie-block' + div.style.display = 'none' + document.body.appendChild(div); + }) + chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + if (request !== 'ok') { + document.getElementById('cookie-block').innerText = JSON.stringify(request) + sendResponse('ok') + } + }) +})(); + +// content.js +window.addEventListener('message', event => { + if (event.source !== window) { + return + } + // 如果是主页面发送message, 则与background通信, 获取页面的 tabId + if (event.data && event.data.hasOwnProperty('type') && event.data.type === 'tab' && event.data.hasOwnProperty('level') && event.data.level === 'main') { + chrome.runtime.sendMessage({ + type: 'tab', + level: 'main' + }, function(response) { + console.log('收到响应', response) + }) + } +}, false) \ No newline at end of file diff --git a/error.png b/error.png new file mode 100644 index 0000000..e89123a Binary files /dev/null and b/error.png differ diff --git a/icon-128.png b/icon-128.png new file mode 100644 index 0000000..d5b1b9f Binary files /dev/null and b/icon-128.png differ diff --git a/icon-16.png b/icon-16.png new file mode 100644 index 0000000..57ca836 Binary files /dev/null and b/icon-16.png differ diff --git a/icon-48.png b/icon-48.png new file mode 100644 index 0000000..8d7ef22 Binary files /dev/null and b/icon-48.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..f47c7a5 --- /dev/null +++ b/manifest.json @@ -0,0 +1,31 @@ +{ + "name": "Cookie与UserAgent获取", + "description": "辅助抓取网站登陆后有效Cookies和UserAgent", + "version": "0.0.3", + "author": "XIAOMO", + "homepage_url": "https://www.xiaoapi.com/", + "permissions": ["contextMenus", "tabs", "storage", "webNavigation", "notifications", "activeTab", "cookies", "", "webRequest", "notifications"], + "icons": { + "16": "icon-16.png", + "48": "icon-48.png", + "128": "icon-128.png" + }, + "background": { + "scripts": ["utils.js"] + }, + "content_scripts": [{ + "matches": [""], + "js": ["content.js"], + "all_frames": true, + "run_at": "document_start" + }], + "browser_action": { + "default_icon": "icon-16.png", + "default_title": "Cookie与UserAgent获取", + "default_popup": "popup.html" + }, + "manifest_version": 2, + "omnibox": { + "keyword": "xiao" + } +} \ No newline at end of file diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..bf67ca8 --- /dev/null +++ b/popup.html @@ -0,0 +1,40 @@ + + + + + + Cookie与UserAgent获取 + + + + +

水滴平台

+
当前登录账号:
+ + + \ No newline at end of file diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..fe80eb1 --- /dev/null +++ b/popup.js @@ -0,0 +1,17 @@ +const bg = chrome.extension.getBackgroundPage() +getTabId() + +function getTabId() { + let urlId = bg.getTabId(); + document.getElementById('current-main').innerText = urlId +} + + +document.addEventListener('DOMContentLoaded', function() { + var cleanBtn = document.querySelector('.clean-btn') + cleanBtn.addEventListener('click', function() { + chrome.tabs.create({ + url: 'http://www.xiaoapi.com/' + }); + }) +}) \ No newline at end of file diff --git a/success.png b/success.png new file mode 100644 index 0000000..069fe77 Binary files /dev/null and b/success.png differ diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..9da1994 --- /dev/null +++ b/utils.js @@ -0,0 +1,129 @@ +let mainPageId = '...' +// 将当前页面的cookies复制到剪切板 +function sendCookies(info, tab) { + let cookies = ''; + chrome.cookies.getAll({ + url: tab.url + }, function(cookie) { + // 遍历当前域名下cookie, 拼接成字符串 + cookie.forEach(v => { + cookies += v.name + "=" + v.value + "; " + }) + // 添加到剪切板 + const input = document.createElement('input'); + input.style.position = 'fixed'; + input.style.opacity = 0; + input.value = cookies; + document.body.appendChild(input) + input.select() + document.execCommand('Copy') + document.body.removeChild(input) + // 传参 + var postData = { + cookie: cookie, + userAgent: navigator.userAgent, + url: location.href + }; + ajax_method('https://pujian.xiaoapi.com/profileCookies', postData, function(data) { + + }) + }) +} + +function getTabId() { + return mainPageId; +} + +function ajax_method(url, data, success) { + // 异步对象 + var ajax = new XMLHttpRequest(); + // post请求 + // post请求 url 是不需要改变 + ajax.open('post', url); + // 需要设置请求报文 + ajax.setRequestHeader("Content-type", "application/json"); + // ajax.setRequestHeader("Access-Control-Allow-Origin", "*"); + // 判断data send发送数据 + if (data) { + // 如果有值 从send发送 + data = JSON.stringify(data); + ajax.send(data); + } else { + // 木有值 直接发送即可 + ajax.send(); + } + // 注册事件 + // alert('注册时间') + ajax.onreadystatechange = function() { + // 在事件中 获取数据 并修改界面显示 + // alert('success') + if (ajax.readyState == 4 && ajax.status == 200) { + // 将 数据 让 外面可以使用 + // return ajax.responseText; + // 当 onreadystatechange 调用时 说明 数据回来了 + // ajax.responseText; + // 如果说 外面可以传入一个 function 作为参数 success + success(ajax.responseText); + } + } +} +var parent = chrome.contextMenus.create({ + "title": "水滴平台", + "contexts": ["page"] +}) +var sendCookies = chrome.contextMenus.create({ + "title": "提取Cookie与UserAgent获取", + "parentId": parent, + "contexts": ["page"], + "onclick": sendCookies +}) +var sendCookies = chrome.contextMenus.create({ + "title": "打开水滴系统", + "parentId": parent, + "contexts": ["page"], + "onclick": function(params) { + chrome.tabs.create({ + url: 'http://www.xiaoapi.com/' + }); + } +}) +var sendCookies = chrome.contextMenus.create({ + "title": "搜狗-微信频道", + "parentId": parent, + "contexts": ["page"], + "onclick": function(params) { + chrome.tabs.create({ + url: 'https://weixin.sogou.com' + }); + } +}) +// +chrome.tabs.onUpdated.addListener(function(id, info, tab) {   + if (tab.status === 'complete' && tab.url.indexOf('https://mp.weixin.qq.com') === 0 && tab.url.indexOf('token=') != -1) { + let cookies = ''; + chrome.cookies.getAll({ + url: tab.url + }, function(cookie) { + // 遍历当前域名下cookie, 拼接成字符串 + cookie.forEach(v => { + cookies += v.name + "=" + v.value + "; " + }) + // 传参 + var postData = { + userAgent: navigator.userAgent, + cookie: cookies, + id: tab.id, + url: tab.url, + title: tab.title, + height: tab.height, + width: tab.width, + status: tab.status, + }; + console.log(postData, tab); + ajax_method('https://pujian.xiaoapi.com/profileCookies', postData, function(data) { + var data = typeof data == 'string' ? JSON.parse(data) : data; + mainPageId = data.message; + }) + }) + }; +}); \ No newline at end of file