1

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

hiddenField1.setAttribute("值", localStorage.curl);

或任何类似的东西,它只是无法加载页面并设置属性。

完整代码:

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();"});
});
4

1 回答 1

0

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

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

顺便说一句,你可以用一行来重现你的错误:

chrome.browserAction.onClicked.addListener(function (t) {
  chrome.tabs.create({"url" : "javascript:localStorage['foo']"});
});
于 2013-07-01T14:48:11.923 回答