2012-01-27 12 views
5

Sto lavorando su un'estensione che analizza il feed rss di gmail per gli utenti. Autorizzo gli utenti a specificare nome utente/password se non vogliono rimanere registrati. Ma questo si interrompe per l'accesso multiplo se l'utente è registrato e il nome utente/password forniti è per un altro account. Quindi voglio evitare di inviare cookie, ma posso comunque inviare il nome utente/password nella chiamata send().C'è un modo per non inviare cookie quando si effettua una richiesta XMLHttpRequest sulla stessa origine?

risposta

3

È possibile farlo utilizzando lo chrome.cookies module. L'idea è di ottenere i biscotti attuali, salvarli, rimuoverli dal negozio di cookie del browser, invia la tua richiesta, e, infine, ripristinarli:

var cookies_temp = []; // where you put the cookies first 
var my_cookie_store = []; // the cookies will be there during the request 
var details = {/*your code*/}; // the first parameter for chrome.cookies.getAll() 
var start_kidnapping = function(cookies) { 
    cookies_temp = cookies.slice(); 
    kidnap_cookie(); 
}; 
var kidnap_cookie = function() { 
    // This recursive function will store the cookies from cookies_temp to 
    // my_cookie_store and then remove them from the browser's cookie store. 
    if (cookies_temp.length == 0) { // when no more cookies, end recursion 
     send_request(); 
    }; 
    else { 
     var cookie = cookies_temp.pop(); 
     // We store url as a property since it is useful later. 
     // You may want to change the scheme. 
     cookie.url = "http://" + cookie.domain + cookie.path; 
     my_cookie_store.push(cookie); // save it 
     chrome.cookies.remove({url: cookie.url, name: cookie.name}, kidnap_cookie); 
    }; 
}; 
var send_request = function() { 
    // Send your request here. It can be asynchronous. 
    for (var i = 0, i < my_cookie_store.length; i++){ 
     delete cookie.hostOnly; // these 2 properties are not part of the 
     delete cookie.session; // object required by chrome.cookies.set() 
     // note that at this point, cookie is no longer a Cookie object 
     chrome.cookies.set(my_cookie_store[i]); // restore cookie 
    }; 
    my_cookie_store = []; // empty it for new adventures 
}; 
chrome.cookies.getAll(details, start_kidnapping); // start 

In alternativa, una soluzione più semplice è quella di aprire una finestra in incognito che invierà la richiesta, utilizzando lo chrome.windows module, ma ciò impedirà di comunicare con il resto dell'estensione. Si noti che potrebbe essere necessario modificare la proprietà incognito del manifesto a split:

var incognito_window = { 
    "url": "incognito.html", 
    "focused": false, // do not bother user 
    "incognito": true 
} 
chrome.windows.create(incognito_window); 
+0

qualora il 'delete cookie.hostOnly,' e 'eliminare cookie.session;' linee effettivamente essere 'eliminare my_cookie_store [i] .hostOnly; 'e' delete my_cookie_store [i] .session; 'rispettivamente? – Mala

4

Come di Chrome 42, l'fetch API consente estensioni di Chrome (e applicazioni web in generale) per eseguire le richieste dei cookie-less. HTML5 Rocks offers an introductory tutorial on using the fetch API.

La documentazione avanzata su fetch è piuttosto scarsa al momento, ma il API interface from the specification è un ottimo punto di partenza. L'algoritmo di recupero descritto sotto l'interfaccia mostra che le richieste generate da fetch non hanno credenziali per impostazione predefinita!

fetch('http://example.com/').then(function(response) { 
    return response.text(); // <-- Promise<String> 
}).then(function(responseText) { 
    alert('Response body without cookies:\n' + responseText); 
}).catch(function(error) { 
    alert('Unexpected error: ' + error); 
}); 

Se si vuole realmente anonimi richieste, si potrebbe anche disattivare la cache:

fetch('http://example.com/', { 
    // credentials: 'omit', // this is the default value 
    cache: 'no-store', 
}).then(function(response) { 
    // TODO: Handle the response. 
    // https://fetch.spec.whatwg.org/#response-class 
    // https://fetch.spec.whatwg.org/#body 
}); 
Problemi correlati