setAttribute未设置为LocalStorage

时间:2013-06-27 20:33:39

标签: google-chrome google-chrome-extension

由于某些原因,我似乎无法将元素的值设置为引号中的文本以外的任何值。我一到:

hiddenField1.setAttribute(“value”,localStorage.curl);

或类似的任何东西都无法加载页面并设置attrib。

完整代码:

var curl = localStorage["curl"];
var username = localStorage["username"];
var password = localStorage["password"];

function fakePost() {  
    var form = document.createElement("form");
    form.setAttribute("name", "form1");
    form.setAttribute("id", "form1");
    form.setAttribute("method", "post");
    form.setAttribute("action", "http://SNIPURLlogin&action=login");

        var hiddenField1 = document.createElement("input");
        hiddenField1.setAttribute("type", "hidden");
        hiddenField1.setAttribute("name", "short");
        hiddenField1.setAttribute("id", "short");
        hiddenField1.setAttribute("value", localStorage["curl"]);
        form.appendChild(hiddenField1);

        var hiddenField2 = document.createElement("input");
        hiddenField2.setAttribute("type", "hidden");
        hiddenField2.setAttribute("name", "txtUsername");
        hiddenField2.setAttribute("id", "txtUsername");
        hiddenField2.setAttribute("value", "shane");
        form.appendChild(hiddenField2);

        var hiddenField3 = document.createElement("input");
        hiddenField3.setAttribute("type", "hidden");
        hiddenField3.setAttribute("name", "txtPassword");
        hiddenField3.setAttribute("id", "txtPassword");
        hiddenField3.setAttribute("value", "shane");
        form.appendChild(hiddenField3);     

    document.body.appendChild(form);

    form.submit();
};
//minify function
fakePostCode = fakePost.toString().replace(/(\n|\t)/gm,'');

chrome.browserAction.onClicked.addListener(function (t) {
  chrome.tabs.create({"url" : "javascript:"+fakePostCode+"; fakePost();"});
});

1 个答案:

答案 0 :(得分:0)

您的问题出现是因为localStorage特定于其来源。这意味着,例如localStorage上的http://example.com值与http://foo.org上的值不同。我们可以说每个来源都“拥有”自己的localStorage

您正在尝试访问localStoage页面的javascript:...对象(或者,至少是about:blank页面)。 Chrome阻止您阅读或写入此特定localStorage,因为您不在“真实”页面上。即使Chrome 没有引发安全错误,您尝试做的事情也没有多大意义 - 为什么您会期望about:blank {{1}要设置?

顺便说一下,您可以用一行重现错误:

localStorage["curl"]