2012-01-27 27 views
5

Tôi đang làm việc trên tiện ích mở rộng phân tích nguồn cấp dữ liệu rmail gmail cho người dùng. Tôi cho phép người dùng chỉ định tên người dùng/mật khẩu nếu họ không muốn duy trì trạng thái đăng nhập. Nhưng điều này vi phạm nhiều lần đăng nhập nếu người dùng đăng nhập và tên người dùng/mật khẩu được cung cấp là cho một tài khoản khác. Vì vậy, tôi muốn tránh gửi bất kỳ cookie nào nhưng vẫn có thể gửi tên người dùng/mật khẩu trong cuộc gọi send().Có cách nào để không gửi cookie khi tạo XMLHttpRequest trên cùng một nguồn gốc không?

Trả lời

3

Bạn có thể làm điều đó bằng cách sử dụng chrome.cookies module. Ý tưởng là để có được các cookie hiện tại, lưu chúng, loại bỏ chúng khỏi cửa hàng cookie của trình duyệt, gửi yêu cầu của bạn, và cuối cùng khôi phục chúng:

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 

Ngoài ra, một giải pháp đơn giản hơn là mở một cửa sổ ẩn danh mà sẽ gửi yêu cầu, sử dụng chrome.windows module, nhưng điều này sẽ ngăn bạn giao tiếp với phần còn lại của tiện ích mở rộng của bạn. Lưu ý rằng bạn có thể phải thay đổi incognito tài sản của biểu hiện của bạn để split:

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

nên 'xóa cookie.hostOnly; 'và' xóa cookie.session; 'dòng thực sự được' xóa my_cookie_store [i] .hostOnly; 'và' xóa my_cookie_store [i] .session; 'tương ứng? – Mala

4

Tính đến Chrome 42, fetch API cho phép phần mở rộng Chrome (và các ứng dụng web nói chung) để thực hiện các yêu cầu cookie ít hơn. HTML5 Rocks offers an introductory tutorial on using the fetch API.

Tài liệu nâng cao trên fetch hiện khá thưa thớt tại thời điểm này, nhưng API interface from the specification là điểm khởi đầu tuyệt vời. Thuật toán tìm nạp được mô tả bên dưới giao diện cho thấy rằng các yêu cầu được tạo bởi fetch không có thông tin xác thực theo mặc định!

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

Nếu bạn muốn yêu cầu hoàn toàn vô danh, bạn cũng có thể vô hiệu hóa bộ nhớ 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 
}); 
Các vấn đề liên quan