Я начал писать расширение chrome для резервного копирования и загрузки файлов cookie. Код в основном работает нормально, но когда я загружаю сохраненные куки, я больше не авторизируюсь. Это проблема на моей стороне, или я не могу войти, установив куки?
Вот мой код для загрузки и хранения куки:
var storeButton = document.getElementById("store");
storeButton.onclick = function() {
chrome.cookies.getAll({}, function(cookies) {
document.getElementById("jar").innerHTML = "";
for(var i = 0; i < cookies.length; i++) {
document.getElementById("jar").innerHTML += cookies[i].name + "<br>";
}
chrome.storage.local.set({cookies: cookies}, function() {
document.getElementById("test").innerHTML = cookies.length + " cookies stored";
});
});
}
function loadCookies(cookies) {
for(var i = 0; i < cookies.length; i++) {
document.getElementById("test").innerHTML = cookies[i].name;
chrome.cookies.set(
{
url: "https://" + (cookies[i].domain.startsWith(".") ? "www" : "") + cookies[i].domain + cookies[i].path,
name: cookies[i].name,
value: cookies[i].value,
secure: cookies[i].secure,
httpOnly: cookies[i].httpOnly,
sameSite: cookies[i].sameSite,
expirationDate: cookies[i].expirationDate,
storeId: cookies[i].storeId
},
function(cookie) {
}
);
}
}
var loadButton = document.getElementById("load");
loadButton.onclick = function() {
chrome.storage.local.get(['cookies'], function(result) {
cookies = result.cookies;
if (cookies == undefined) {
document.getElementById("test").innerHTML = "No cookies stored";
return;
}
chrome.cookies.getAll({}, function(oldCookies) {
if (oldCookies.length == 0) {
loadCookies(cookies);
}
var removedCookies = 0;
for(var i = 0; i < oldCookies.length; i++) {
chrome.cookies.remove({url: "https://" + oldCookies[i].domain + oldCookies[i].path, name: oldCookies[i].name}, function() {
removedCookies++;
if (removedCookies >= oldCookies.length) {
loadCookies(cookies);
}
});
}
});
});
}
The problem might be this line of code, because I'm automatically adding the "www" subdomain when no subdomain is specified in the domain of the cookie.
I didn't know how to construct the url properly.
url: "https: // "+ (cookies [i] .domain.startsWith (". ")?" www ":" ") + cookies [i] .domain + cookies [i] .path