You can save the token as a cookie . Cookies work like localStorage, but in addition, they are also included by default in every HTTP request to the server. And here is the trick. The Chrome extension can access HTTP requests using the webRequestAPI. This way, he can look into the request headers and find out your cookies. Having this token as a cookie makes it accessible for extension.
, , HTTP- , ? , . ajax .
, , :
:
"permissions": [
"webRequest",
"webRequestBlocking",
"*://*.my_site.com/*"
]
:
function callback (details) {
token = func_extract_token_from_headers(details.requestHeaders);
chrome.webRequest.onBeforeSendHeaders.removeListener(callback);
return {cancel: false}
}
chrome.webRequest.onBeforeSendHeaders.addListener (callback,
{urls: ["http://www.my_site.com/*", "https://www.my_site.com/*"]},
["blocking", "requestHeaders"]);
var xurl = "https://www.my_site.com/";
var xhr = new XMLHttpRequest();
xhr.open("GET", xurl, true);
xhr.send();
, , - CSP - Content Secutiry Policiy. - iframe , wOxxOm , , CSP whitelisting -. . ,
EDIT:
, : iframes, , . ( ), CSP - .
iframe, . window.postMessage API
, , :
:
"content_security_policy": "script-src 'self' https://my_site.com/; object-src 'self'"
"background": { "page": "background.html"}
:
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event)
{
if(event.origin == "https://my_site.com");
chrome.storage.storage.local.set({'auth_token': event.data});
}
iframe = document.createElement('iframe');
iframe.src = "https://my_site.com/";
document.getElementById('div').appendChild(iframe);
iframe.contentWindow.postMessage("give_token", "https://my_site.com")
-:
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event)
{
if(event.origin == "your_extension_id_aeiou12345");
event.source.postMessage(''+localStorage.auth_token, event.origin);
}
EDIT:
, - iframe, , X-frame-options . , , URL- .