Files
temu-plugin/src/entry/background.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-08-06 16:50:17 +08:00
/**
利用chrome的fetch来避免跨域
**/
import {genAnti} from "@/entry/genAnti";
2023-08-06 16:50:17 +08:00
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type == 'api') {
new Promise((resolve, reject) => {
let headers = {};
2023-08-06 16:50:17 +08:00
if (request.needMallId) {
headers.Mallid = request.mallId;
}
headers['Content-Type'] = 'application/json';
headers.cookie = getCookie();
// Promise.resolve(genAnti()).then(r => {
// headers["Anti-Content"] = r
// })
Promise.resolve().then(() => fetch(request.url, {
2023-08-06 16:50:17 +08:00
'headers': headers,
'method': 'POST',
'referrerPolicy': 'no-referrer',
'credentials': 'include',
'body': JSON.stringify(request.data),
'mode': 'cors'
})).then((res) => {
2023-08-06 16:50:17 +08:00
resolve(res.json());
});
}).then((res) => {
sendResponse(res);
});
} else if (request.type == 'init') {
fetch(request.url,{headers:{cookie: getCookie()}}).then(res => sendResponse(res.text()));
2023-08-06 16:50:17 +08:00
} else if (request.type == 'notify') {
chrome.notifications.create(
"" + Math.random(), {
type: "basic",
title: "TEMU助手",
message: "您店铺【" + request.mallName + "】的商品【" + request.productName + "】成功加入发货台,请尽快处理",
iconUrl: "./icons/48.png"
}, null
2023-08-06 16:50:17 +08:00
)
}
2023-08-06 16:50:17 +08:00
return true;
});
chrome.action.onClicked.addListener(function () {
chrome.tabs.create({
url: "./popup.html"
2023-08-06 16:50:17 +08:00
}, function (tab) {
console.log('tab is:' + tab);
2023-08-06 16:50:17 +08:00
});
});
function getCookie() {
const url = new URL("https://kuajing.pinduoduo.com/");
let cStr = '';
chrome.cookies.getAll({domain: url.host}, (cookie) => {
cookie.map((c) => {
cStr += c.name + '=' + c.value + ';';
});
});
return cStr;
}