{ ... "permissions": [ "cookies", ... ], ... }
chrome.cookies.get(details, callback)
for getting data about one cookie, a similar getAll
method for getting data about all cookies (with the same parameters, but details work as a filter, it turned out to be very convenient) , and chrome.cookies.set(details, callback)
. I will not popularly describe the parameters and use, you can read about it in the documentation . It’s better to go straight to the point. function deleteCookies() { // cookies for (var i = 0; i < .length; i++) { try { var d = [i]; var u = ((d.secure) ? "https://" : "http://") + d.domain + d.path; chrome.cookies.remove({ url: u, name: d.name, storeId: d.storeId }); } catch (e) { console.error("Error catched deleting cookie:\n" + e.description); } } } function setCookies(c) { // cookies for (var i = 0; i < c.length; i++) { try { var d = c[i]; var b = { url: ((d.secure) ? "https://" : "http://") + d.domain, name: d.name, storeId: d.storeId, value: d.value, path: = d.path, secure: = d.secure, httpOnly: = d.httpOnly, }; if (!d.hostOnly) { b.domain = d.domain; } if (!d.session) { b.expirationDate = d.expirationDate; } chrome.cookies.set(b); } catch (e) { console.error("Error setting cookie:\n" + e.description) } } }
chrome.cookies.getAllCookieStores(callback)
method, in response to which something like this is returned: [{ id: 0, tabIds: [1,2,3,4], },]
But always returns 1 or 2 objects: one normal, and one for the incognito window (if it is open, and the extension is given access to this window). Judging by the presence of the tabIds parameter, it was either planned and forgotten, or else the possibility of managing these stores and linking tabs to a specific store is planned. If it already worked, it would help in the implementation of the idea, which will be discussed in the next section. var profiles = JSON.parse(localStorage.profiles); // var inuse = localStorage.inuse; // var cookies = ['facebook.com','secure.facebook.com','on.fb.me']; //, cookies function newProfile(){ // cookies var ii = 0; // cookies callback, , var initer = function(cii){ chrome.cookies.getAll({domain:cookies[cii]}, function (f){ var nc = profiles.length-1; profiles[nc].cookies = profiles[nc].cookies.concat(f); if (cii<(cookies.length-1)){ cii++; initer(cii); } else{ saveSettings(); //... } }); } chrome.cookies.getAll({domain:cookies[0]}, function (f){ var nc = profiles.length; profiles[nc] = {title:'New profile '+nc,cookies:f}; inuse = nc; if (ii<(cookies.length-1)){ ii++; initer(ii); } else{ saveSettings(); //... } }); } function switchProfile(d) { // if (profiles[d] != undefined && profiles[d] != null && d != inuse) { //cookies, , . cookies // var deleter = function(cii){ chrome.cookies.getAll({domain:cookies[cii]}, function (f){ deleteCookies(f); if (cii < (cookies.length-1)){ cii++; deleter(cii); } else{ restoreCookies(profiles[d].cookies); inuse = d; saveSettings(); //...+ } }); } deleter(0); } }
{ ... "permissions": [ "webRequest", "webRequestBlocking" ], ... }
var tabs = {}; // function openProfile(id,url){ // if (url.indexOf('#')>-1) url = url.substr(0,url.indexOf('#')+1) + encodeURIComponent(url.substr(url.indexOf('#')+1)); // , - , loadSettings(); chrome.tabs.create({url:url},function(r){ // if (r) tabs[r.id] = new Array(tinuse,id,profiles[id].cookies.slice()); }); } chrome.webRequest.onBeforeSendHeaders.addListener(function(d){ if (d.tabId){ // td = d.tabId; if (tabs[td]){ // td = tabs[td]; for (var i = 0; i < d.requestHeaders.length; i++){ if (d.requestHeaders[i].name=='Cookie'){ var cks = d.requestHeaders[i].value.split('; '); // Cookies for (var j = 0; j < cks.length; j++){ cks[j] = cks[j].split('='); for (var k = 0; k < td[2].length; k++){ if (td[2][k].name==cks[j][0]){ // cks[j][1] = td[2][k].value; break; } } cks[j] = cks[j].join('='); } d.requestHeaders[i].value = cks.join('; '); // cookies } } } } return {requestHeaders: d.requestHeaders}; // },{ urls: ["<all_urls>"] // . , },[ "blocking", // "requestHeaders" // ]);
chrome.webRequest.onHeadersReceived.addListener(function(d){ if (d.tabId){ td = d.tabId; if (tabs[td]){ td = tabs[td]; //Cookies : //name: "Set-Cookie" //value: "cookiename=value; expires=Sun, 22-Dec-2013 03:31:23 GMT; path=/; domain=.domain.com" for (var i = 0; i < d.responseHeaders.length; i++){ if (d.responseHeaders[i].name=='Set-Cookie'){ var sk = d.responseHeaders[i].value.split('; '); sk[0] = sk[0].split('='); var ck = {name:sk[0][0],value:sk[0][1]}; for (var j = 1; j < sk.length; j++){ sk[j] = sk[j].split('='); ck[sk[j][0]] = sk[j][1]; } for (var k = 0; k < td[2].length; k++){ if (td[2][k].name==ck.name && td[2][k].path==ck.path && td[2][k].domain==ck.domain){ // cookie, td[2][k].value = ck.value; break; } } // , cookies , d.responseHeaders.splice(i,1); i--; } } } } return {responseHeaders: d.responseHeaders}; },{ urls: ["<all_urls>"] },[ "blocking", "responseHeaders" ]);
chrome.tabs.onCreated.addListener(function(tab){ if (tab.openerTabId){ // - - if (tabs[tab.openerTabId]){ tabs[tab.id] = tabs[tab.openerTabId]; } } }); chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) { // . - ID – if (tabs[tabId]) delete tabs[tabId]; });
Source: https://habr.com/ru/post/171959/
All Articles