从 localStorage 获取值以在 popup.html 中使用

Get value from localStorage to use in popup.html

我正在创建我的第一个 chrome 扩展,并且已经到了我似乎无法弄清楚的阶段。

基本上我可以选择保存,但我不知道如何检索值以在 popup.html

中使用它

这是我的代码:

manifest.json

{
"manifest_version": 2,
"name": "My Extension",
"options_page": "options.html",

"permissions": [
      "storage"
    ],

"description": "My Extension",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
   }
}

options.js

// Save options to localStorage.
function save_options()
{
  localStorage["url"] = document.getElementById("url").value;

  // Let the user know the options were saved.
  var status = document.getElementById("status");
  status.innerHTML = "Saved OK";
  setTimeout( function() { status.innerHTML = ""; }, 5000 );
}

// Populate options from from localStorage.
function load_options()
{
  var url = localStorage["url"];
  if (url == null) { url = ""; }
  document.getElementById("url").value = url;
}

// call load_options top populate the GUI when the DOM has loaded
document.addEventListener('DOMContentLoaded', load_options);

// call save_options when the user clicks the save button
document.getElementById("save").addEventListener('click', save_options);

options.html

<html>
<head>
<head><title>My Options</title></head>
<body>
    <div class="container">

        <label for="url">
            <p>My URL</p>
            <input type="text" id="url" />
            <button id="save">Save</button>
        </label>

    </div>

    <div id="status"></div>

</body>
<script src="options.js"></script>
</html>

popup.html

<html>
<head>
</head>
<body>
<iframe src="myURL" id="myURL" name="myURL" width="200" height="200"></iframe>
</body>
</html>

这很好用,在选项中,您可以保存在 url 字段中输入的任何内容。现在我想获取保存的值并将其作为 popup.html

中 iframe 的 src (myURL)

示例:因此,如果您在选项中输入并保存 http://www.google.com,那么在 popup.html 中,iframe 将是:

<iframe src="http://www.google.com" id="myURL" name="myURL" width="200" height="200"></iframe>

感谢任何帮助。

要获取本地存储值,您可以执行

savedURL = localStorage.get('url') ;

然后在jQuery里面是这样的

$('#myURL').attr('src',savedURL)

基本问题,但你知道了。

  1. 显然,为此您需要 JavaScript 代码。 Chrome 的内容安全策略禁止内联代码,因此代码必须进入单独的 .js 文件(例如 popup.js)并包含在 HTML:

     <html>
       <head>
         <script src="popup.js"></script>
       </head>
       <body>
         <iframe id="myURL" name="myURL" width="200" height="200"></iframe>
       </body>
     </html>
    
  2. 代码应该:

一个。从 localStroage

读取值

b。查找页面上的元素

c。分配其 src 属性.

    // Read the value
    var url = localStorage.url;
      // or = localStorage["url"];
      // or = localStorage.get("url");
    
    // Find the element  
    var element = document.getElementById("myUrl");

    // Assign the property
    element.src = url;
     // or .setAttribute("src", url);

当然,所有这些都可以通过删除临时变量 urlelement(见下文)在一行代码中进行压缩。

  1. 但是,这有一个问题。代码将在读取 <script> 元素时执行 - 在读取 #myURL 元素之前。因此,它将失败,因为 element 将找不到并且将是未定义的。

有两种修复方法:

一个。将 <script> 标签移动到它所指的元素下方,就像您在 options.html

中所做的那样

b。将您的代码包装在一个监听器中,以获取页面已完全解析并准备就绪的事件。我更喜欢这个解决方案。

    document.addEventListener("DOMContentLoaded", function() {
      document.getElementById("myUrl").src = localStorage.url;
    });

进一步阅读: