我正在尝试实现自己的chrome扩展程序,该扩展程序允许用户无需单击扩展程序图标即可登录系统。
manifest.json:
{
"name": "form",
"description": "form",
"version": "1.1",
"manifest_version": 2,
"content_scripts": [
{
"matches": [
"https://www.example.com/*",
"http://www.example.com/*"
],
"js": [
"js/jquery-2.2.4.min.js", "js/modal.js"
]
}
],
"permissions": [
"storage", "tabs", "cookies", "*://www.example.com/*"
],
"background": {
"scripts": ["js/background.js"]
}
}
popup.js:
wrapperDiv = document.createElement("div");
wrapperDiv.setAttribute("id", "wrapper");
wrapperDiv.setAttribute("style","position: absolute; left: 0px; top: 0px; background-color: rgb(160, 160, 160); opacity: 0.5; z-index: 2000; height: 100%; width: 100%;");
iframeElement = document.createElement("iframe");
iframeElement.setAttribute("style","width: 100%; height: 100%;");
wrapperDiv.appendChild(iframeElement);
modalDialogParentDiv = document.createElement("div");
modalDialogParentDiv.setAttribute("id", "authModal");
modalDialogParentDiv.setAttribute("style","position: fixed; width: 500px; border: 1px solid rgb(51, 102, 153); background-color: rgb(255, 255, 255); z-index: 2001; overflow: auto; text-align: center; top: 149px; left: 497px;");
modalDialogSiblingDiv = document.createElement("div");
modalDialogTextDiv = document.createElement("div");
modalDialogTextDiv.setAttribute("style" , "text-align:center");
modalDialogTextSpan = document.createElement("h2");
modalDialogTextSpan.setAttribute("style","margin-bottom:50px; margin-top:0px; font-size: 20px !important; font-weight: 700; background-color: #435f71;color: #fff; padding: 10px 20px;");
modalDialogText = document.createElement("strong");
modalDialogText.innerHTML = "Formulaire de Connexion";
labelErreur=document.createElement("p");
labelErreur.setAttribute("id", "erreur");
labelErreur.setAttribute("style", "color:red; margin-bottom:10px;");
labelLogin=document.createElement("label");
breakElement1 = document.createElement("br");
labelLogin.innerHTML="Nom d'utilisateur: ";
loginElement = document.createElement("input");
loginElement.setAttribute("style", "border-radius: 5px;");
loginElement.setAttribute("id", "login");
loginElement.setAttribute("type", "text");
loginElement.setAttribute("name", "login");
loginElement.setAttribute("required", "");
breakElement2 = document.createElement("br");
labelMdp=document.createElement("label");
breakElement3 = document.createElement("br");
labelMdp.innerHTML="Mot de passe: ";
mdpElement = document.createElement("input");
mdpElement.setAttribute("style", "border-radius: 5px;");
mdpElement.setAttribute("id", "mdp");
mdpElement.setAttribute("type", "text");
mdpElement.setAttribute("name", "mdp");
mdpElement.setAttribute("required", "");
breakElement4 = document.createElement("br");
submitElement = document.createElement("button");
submitElement.setAttribute("style", "border-radius: 5px; margin-top:10px; margin-bottom:50px;");
submitElement.setAttribute("id", "valid");
submitElement.setAttribute("type", "submit");
submitElement.setAttribute("name", "valid");
submitElement.innerHTML="Valider";
modalDialogTextSpan.appendChild(modalDialogText);
modalDialogTextDiv.appendChild(modalDialogTextSpan);
modalDialogTextDiv.appendChild(labelErreur);
modalDialogTextDiv.appendChild(labelLogin);
modalDialogTextDiv.appendChild(breakElement1);
modalDialogTextDiv.appendChild(loginElement);
modalDialogTextDiv.appendChild(breakElement2);
modalDialogTextDiv.appendChild(labelMdp);
modalDialogTextDiv.appendChild(breakElement3);
modalDialogTextDiv.appendChild(mdpElement);
modalDialogTextDiv.appendChild(breakElement4);
modalDialogTextDiv.appendChild(submitElement);
modalDialogSiblingDiv.appendChild(modalDialogTextDiv);
modalDialogParentDiv.appendChild(modalDialogSiblingDiv);
$("body").append(wrapperDiv);
$("body").append(modalDialogParentDiv);
单击提交按钮时,我使用ajax发送数据以获取响应(用户ID) 我想记住(在身份验证表单中记住我),因为我已经将此user_id发送到background.js并将其设置在cookie中:
background.js:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
chrome.cookies.set({
"name": "identifiant",
"url": "https://www.example.com/*",
"value": request
}, function (cookie) {
console.log(JSON.stringify(cookie));
console.log(chrome.extension.lastError);
console.log(chrome.runtime.lastError);
});
});
chrome.cookies.get({"url": "https://www.example.com/*", "name": "identifiant"}, function(cookie) {
if (cookie && cookie.value!=null) {
ID = cookie.value;
}
});
现在我需要在popup.js上取回此cookie,以在显示模式之前检查用户是否登录