Reputation: 18735
How can I set a cookie using Chrome Extension? I'm working on a login to my web page through Chrome Extension. I have to set sessionid
but it does not work.
The alert in success
function is visible but then, it doesn't log in nor redirects page.
$(document).ready(function () {
$('#login-button-id').click(function () {
var login = $('#input-email-id').val();
var password = $('#input-password-id').val();
$.ajax({
url: 'http://127.0.0.1:8000/api/authenticate',
type: 'POST',
data: {
'login': login,
'password': password
},
success: function (data) {
$.cookie('sessionid',data.sessionid);
chrome.cookies.set({"sessionid":data.sessionid});
alert("You are logged in");
window.location.href = "http://127.0.0.1:8000/admin";
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("JQuery failed: " + textStatus + " with error thrown: " + errorThrown);
console.log(jqXHR);
}
})
});
$('#logout-button-id').click(function () {
})
});
MANIFEST
{
"manifest_version": 2,
"name": "xyz",
"description": "This is a plugin collaborating with xyz.com",
"version": "1.0",
"browser_action": {
"default_icon": "spy-icon.png",
"default_popup": "popup.html",
"default_title": "Click here!"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"cookies",
"<all_urls>"
],
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
}
I suppose it's because Chrome Extension is a sandbox so it has it's own cookies but can't figure out what to do, any suggestions?
Upvotes: 1
Views: 6557
Reputation: 77531
I suppose it's because Chrome Extension is a sandbox so it has it's own cookies
Indeed, since you're trying to use this from an extension's own page, that binds the cookie you're trying to set via $.cookie
to extension's own origin (chrome-extension://somelongextensionidhere/
). So this doesn't work.
As for chrome.cookies.set
, you need to read documentation on how you use it!
Proper invocation should be something like this:
chrome.cookies.set({
url: "http://127.0.0.1:8000/",
name: "sessionid",
value: data.sessionid
});
Upvotes: 2