This commit is contained in:
小陌 2023-04-07 14:19:41 +08:00
commit a356577360
10 changed files with 247 additions and 0 deletions

30
content.js Normal file
View File

@ -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)

BIN
error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
icon-128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

BIN
icon-16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
icon-48.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

31
manifest.json Normal file
View File

@ -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", "<all_urls>", "webRequest", "notifications"],
"icons": {
"16": "icon-16.png",
"48": "icon-48.png",
"128": "icon-128.png"
},
"background": {
"scripts": ["utils.js"]
},
"content_scripts": [{
"matches": ["<all_urls>"],
"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"
}
}

40
popup.html Normal file
View File

@ -0,0 +1,40 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cookie与UserAgent获取</title>
<style type="text/css">
body {
margin: 10px;
white-space: nowrap;
}
h1 {
font-size: 15px;
}
.container {
align-items: center;
display: flex;
justify-content: space-between;
}
.clean-btn {
margin-left: 20px;
padding: 5px 10px;
color: #fff;
border: 0;
background: #00AEF0;
}
.clean-btn:hover {
background: rgb(27, 177, 236);
}
</style>
</head>
<body>
<h1>水滴平台</h1>
<div>当前登录账号: <span id="current-main"></span> <button class="clean-btn">前往网站</button></div>
<script src="popup.js"></script>
</body>
</html>

17
popup.js Normal file
View File

@ -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/'
});
})
})

BIN
success.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

129
utils.js Normal file
View File

@ -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;
})
})
};
});