2023-08-06 16:50:17 +08:00
|
|
|
/**
|
2023-08-14 18:25:19 +08:00
|
|
|
利用chrome的fetch来避免跨域
|
|
|
|
|
**/
|
2023-08-06 16:50:17 +08:00
|
|
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
|
|
|
if (request.type == 'api') {
|
2023-08-15 17:09:00 +08:00
|
|
|
new Promise((resolve) => {
|
2023-08-06 16:50:17 +08:00
|
|
|
let headers = {};
|
|
|
|
|
if (request.needMallId) {
|
|
|
|
|
headers.Mallid = request.mallId;
|
|
|
|
|
}
|
2023-08-15 17:09:00 +08:00
|
|
|
if (request.anti) {
|
2023-08-17 02:41:13 +08:00
|
|
|
headers["Anti-Content"] = request.anti
|
2023-08-15 17:09:00 +08:00
|
|
|
}
|
2023-08-06 16:50:17 +08:00
|
|
|
headers['Content-Type'] = 'application/json';
|
|
|
|
|
headers.cookie = getCookie();
|
2023-08-14 18:25:19 +08:00
|
|
|
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'
|
2023-08-14 18:25:19 +08:00
|
|
|
})).then((res) => {
|
2023-08-06 16:50:17 +08:00
|
|
|
resolve(res.json());
|
|
|
|
|
});
|
2023-08-15 17:09:00 +08:00
|
|
|
}).then(sendResponse);
|
2023-08-06 16:50:17 +08:00
|
|
|
} else if (request.type == 'notify') {
|
|
|
|
|
chrome.notifications.create(
|
2023-08-14 18:25:19 +08:00
|
|
|
"" + 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-14 18:25:19 +08:00
|
|
|
|
2023-08-06 16:50:17 +08:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chrome.action.onClicked.addListener(function () {
|
|
|
|
|
chrome.tabs.create({
|
2023-08-14 18:25:19 +08:00
|
|
|
url: "./popup.html"
|
2023-08-06 16:50:17 +08:00
|
|
|
}, function (tab) {
|
2023-08-14 18:25:19 +08:00
|
|
|
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;
|
2023-08-14 18:25:19 +08:00
|
|
|
}
|