有没有办法在同一个源上创建XMLHttpRequest时不发送cookie?

Shr*_*rey 7 javascript ajax xmlhttprequest google-chrome-extension

我正在开发一个解析用户的gmail rss feed的扩展.我允许用户指定用户名/密码,如果他们不想保持登录状态.但如果用户已登录且提供的用户名/密码用于其他帐户,则会中断多次登录.所以我想避免发送任何cookie,但仍然可以在send()调用中发送用户名/密码.

Rob*_*b W 5

从Chrome 42开始,fetchAPI允许Chrome扩展程序(和一般的Web应用程序)执行无cookie请求。HTML5 Rocks提供了有关使用访存API的入门教程

目前有关高级文档的信息fetch还很少,但是规范中API接口是一个很好的起点。界面下方描述的提取算法显示,fetch默认情况下,生成的请求没有凭据!

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);
});
Run Code Online (Sandbox Code Playgroud)

如果您想要真正的匿名请求,则还可以禁用缓存:

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
});
Run Code Online (Sandbox Code Playgroud)


emm*_*emm 3

您可以使用chrome.cookies 模块来做到这一点。这个想法是获取当前的cookie,保存它们,从浏览器的cookie存储中删除它们,发送您的请求,最后恢复它们:

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
Run Code Online (Sandbox Code Playgroud)

或者,一个更简单的解决方案是打开一个隐身窗口,该窗口将使用chrome.windows 模块发送请求,但这将阻止您与扩展程序的其余部分进行通信。请注意,您可能需要将incognito清单的属性更改为split

var incognito_window = {
    "url": "incognito.html",
    "focused": false, // do not bother user
    "incognito": true
}
chrome.windows.create(incognito_window);
Run Code Online (Sandbox Code Playgroud)