2012-01-27 8 views
5

私はgmailのRSSフィードを解析する拡張機能に取り組んでいます。私は、ユーザーがサインインしたままにしたくない場合は、ユーザー名/パスワードを指定することができます。しかし、ユーザーがサインインしていて、提供されているユーザー名/パスワードが別のアカウントのものであれば、これは複数のサインインのために破棄されます。だから、私はクッキーを送信することを避けたいが、send()コールでユーザー名/パスワードを送信できるようにしたい。XMLHttpRequestを同じ起点にするときにクッキーを送信しない方法はありますか?

答えて

3

これは、chrome.cookies moduleを使用して行うことができます。アイデアは、現在のクッキーを取得し、それらを保存し、ブラウザの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を使用していますが、これにより残りの内線番号との通信ができなくなります。あなたがsplitにマニフェストのincognitoプロパティを変更する必要があることに注意してください:クローム42のよう

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

を;'と 'cookie.sessionを削除;'ラインが実際に 'my_cookie_storeを削除します[i] .hostOnly; 'および' delete my_cookie_store [i] .session; '? – Mala

4

fetch APIはCookieのないリクエストを実行するにはChromeの拡張機能(および一般的なWebアプリケーションを)ことができます。 HTML5 Rocks offers an introductory tutorial on using the fetch API

fetchに関する詳細なドキュメントは現時点ではまばらですが、API interface from the specificationはすばらしい出発点です。インターフェイスの下に記載されているフェッチアルゴリズムは、デフォルトで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); 
}); 

あなたが本当に匿名の要求をしたい場合、あなたはまた、キャッシュを無効にすることができます: `cookie.hostOnlyを削除する必要があります

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 
}); 
関連する問題