2012-01-27 12 views
5

मैं ऐसे एक्सटेंशन पर काम कर रहा हूं जो उपयोगकर्ताओं के लिए जीमेल आरएसएस फ़ीड को पार करता है। यदि उपयोगकर्ता साइन-इन नहीं रहना चाहते हैं तो मैं उपयोगकर्ता को उपयोगकर्ता नाम/पासवर्ड निर्दिष्ट करने की अनुमति देता हूं। लेकिन यदि उपयोगकर्ता साइन-इन है और प्रदान किया गया उपयोगकर्ता नाम/पासवर्ड एक अलग खाते के लिए है, तो यह एकाधिक साइन-इन के लिए टूट जाता है। इसलिए मैं किसी भी कुकीज़ को भेजने से बचना चाहता हूं लेकिन फिर भी भेजें() कॉल में उपयोगकर्ता नाम/पासवर्ड भेज सकता हूं।क्या एक ही उत्पत्ति पर XMLHttpRequest बनाते समय कुकीज़ भेजने का कोई तरीका नहीं है?

उत्तर

3

आप chrome.cookies module का उपयोग करके ऐसा कर सकते हैं। विचार वर्तमान कुकीज़,,, प्राप्त करने के लिए उन्हें बचाने के लिए उन्हें ब्राउज़र की कुकी की दुकान से हटाने के अपने अनुरोध भेजने के लिए, और अंत में उन्हें बहाल है: वैकल्पिक रूप से

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 का उपयोग करके, लेकिन यह आपको अपने शेष एक्सटेंशन के साथ संवाद करने से रोक देगा। ध्यान दें कि आप split अपने मेनिफ़ेस्ट की incognito संपत्ति को बदलने के लिए हो सकता है:

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

'cookie.hostOnly हटाना चाहिए,' और 'cookie.session हटाना;' लाइनों वास्तव में 'हो my_cookie_store हटाना [i] .होस्ट केवल; 'और 'my_cookie_store [i]। सत्र;' क्रमशः हटाएं? – Mala

4

क्रोम 42 के रूप में, fetch API क्रोम एक्सटेंशन (और सामान्य रूप में वेब अनुप्रयोगों) कुकी-कम अनुरोध पर कार्रवाई करने की अनुमति देता है। HTML5 Rocks offers an introductory tutorial on using the fetch API

fetch पर उन्नत प्रलेखन इस समय काफी स्पैस है, लेकिन API interface from the specification एक महान प्रारंभिक बिंदु है। इंटरफ़ेस के नीचे वर्णित 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 
}); 
संबंधित मुद्दे

 संबंधित मुद्दे