feat:增加cookie发送功能
This commit is contained in:
parent
55c3b548ca
commit
9f758cd885
|
|
@ -17,8 +17,81 @@ chrome.runtime.onInstalled.addListener(function () {
|
||||||
title: "Send page to MeTube",
|
title: "Send page to MeTube",
|
||||||
contexts: ['page'],
|
contexts: ['page'],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
chrome.contextMenus.create({
|
||||||
|
id: 'metube-cookie',
|
||||||
|
title: "Send cookie to MeTube",
|
||||||
|
contexts: ['page'],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function sendCookieToMetube(pageUrl, metubeUrl, callback) {
|
||||||
|
console.log("[Cookie] Starting to send cookie for " + pageUrl + " to metubeUrl=" + metubeUrl);
|
||||||
|
|
||||||
|
if (typeof callback !== 'function') {
|
||||||
|
callback = function () {};
|
||||||
|
}
|
||||||
|
|
||||||
|
let urlObj = new URL(pageUrl);
|
||||||
|
let domain = urlObj.hostname;
|
||||||
|
console.log("[Cookie] Domain extracted: " + domain);
|
||||||
|
|
||||||
|
// Get all cookies for the current domain
|
||||||
|
chrome.cookies.getAll({ domain: domain }, function(cookies) {
|
||||||
|
console.log("[Cookie] Retrieved " + (cookies ? cookies.length : 0) + " cookies for domain: " + domain);
|
||||||
|
|
||||||
|
if (!cookies || cookies.length === 0) {
|
||||||
|
console.log("[Cookie] No cookies found for domain: " + domain);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format cookies as a string (name=value; name2=value2)
|
||||||
|
let cookieString = cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; ');
|
||||||
|
|
||||||
|
console.log("[Cookie] Cookie string length: " + cookieString.length);
|
||||||
|
console.log("[Cookie] Cookie preview: " + cookieString.substring(0, 100) + "...");
|
||||||
|
|
||||||
|
// Base64 encode the cookie string to handle special characters
|
||||||
|
let cookieBase64 = btoa(unescape(encodeURIComponent(cookieString)));
|
||||||
|
console.log("[Cookie] Base64 encoded cookie length: " + cookieBase64.length);
|
||||||
|
|
||||||
|
let postData = {
|
||||||
|
"url": pageUrl,
|
||||||
|
"cookie": cookieBase64,
|
||||||
|
"domain": domain
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("[Cookie] Sending POST to: " + metubeUrl + "/cookie");
|
||||||
|
console.log("[Cookie] Post data:", postData);
|
||||||
|
|
||||||
|
fetch(metubeUrl + "/cookie", {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(postData)
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
console.log("[Cookie] Response status: " + response.status);
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(function (response) {
|
||||||
|
console.log("[Cookie] Response data:", response);
|
||||||
|
if (response.status === 'ok') {
|
||||||
|
console.log("[Cookie] Cookie sent successfully");
|
||||||
|
callback();
|
||||||
|
} else {
|
||||||
|
console.error("[Cookie] Unexpected response status:", response);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
console.error("[Cookie] ERROR: " + e);
|
||||||
|
console.error("[Cookie] Error details:", e);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function sendVideoUrlToMetube(videoUrl, metubeUrl, format, advancedSettings, callback) {
|
function sendVideoUrlToMetube(videoUrl, metubeUrl, format, advancedSettings, callback) {
|
||||||
console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl);
|
console.log("Sending videoUrl=" + videoUrl + " to metubeUrl=" + metubeUrl);
|
||||||
|
|
||||||
|
|
@ -68,6 +141,20 @@ chrome.contextMenus.onClicked.addListener(function (item, tab) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle cookie menu item
|
||||||
|
if (item.menuItemId === 'metube-cookie') {
|
||||||
|
console.log("[Menu] Cookie menu clicked");
|
||||||
|
console.log("[Menu] item.pageUrl:", item.pageUrl);
|
||||||
|
console.log("[Menu] tab.url:", tab.url);
|
||||||
|
let pageUrl = item.pageUrl || tab.url;
|
||||||
|
console.log("[Menu] Using pageUrl:", pageUrl);
|
||||||
|
console.log("[Menu] MeTube URL:", data.metube);
|
||||||
|
sendCookieToMetube(pageUrl, data.metube, function () {
|
||||||
|
console.log("[Menu] Cookie send callback completed for: " + pageUrl);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let needToSwitch = (data.contextMenuClickBehavior === 'context-menu-send-current-url-and-switch');
|
let needToSwitch = (data.contextMenuClickBehavior === 'context-menu-send-current-url-and-switch');
|
||||||
|
|
||||||
let videoUrl = item.linkUrl || item.pageUrl;
|
let videoUrl = item.linkUrl || item.pageUrl;
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
"description": "Use the context menu to send video into MeTube application",
|
"description": "Use the context menu to send video into MeTube application",
|
||||||
"version": "1.7",
|
"version": "1.7",
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"permissions": ["contextMenus", "storage", "tabs"],
|
"permissions": ["contextMenus", "storage", "tabs", "cookies"],
|
||||||
|
"host_permissions": ["<all_urls>"],
|
||||||
"background": {
|
"background": {
|
||||||
"service_worker": "background.js"
|
"service_worker": "background.js"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue