在同源策略下,是否有不发送cookie的方式进行XMLHttpRequest?

7
我正在开发一个扩展程序,用于解析 Gmail RSS 订阅源。如果用户不想保持登录状态,我允许他们指定用户名和密码。但是,如果用户已登录且所提供的用户名和密码属于不同的帐户,则会出现问题。因此,我想避免发送任何 cookie,但仍能够在 send() 调用中发送用户名和密码。
2个回答

5
从Chrome 42开始,fetch API允许Chrome扩展程序(以及Web应用程序)执行无需Cookie的请求。HTML5 Rocks提供有关使用fetch API的入门教程
目前,fetch的高级文档非常稀少,但是规范中的API接口是一个很好的起点。接口下面描述的fetch算法表明,默认情况下由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);
});

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

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
});

3
你可以使用chrome.cookies模块来实现这一点。思路是获取当前的cookies,保存它们,从浏览器的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

另外,一个更简单的解决方案是打开一个隐身窗口,使用 chrome.windows module 发送请求,但这将阻止您与扩展程序的其他部分通信。请注意,您可能需要将清单文件中的 incognito 属性更改为 split

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

这两行代码 delete cookie.hostOnly;delete cookie.session; 是否应该分别改为 delete my_cookie_store[i].hostOnly;delete my_cookie_store[i].session; - Mala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接