javascript - 在 firefox 附加面板中显示远程图像

标签 javascript firefox firefox-addon firefox-addon-sdk

我正在尝试在 FireFox 附加面板中显示远程图像,但是 src 属性正在从类似这样的内容转换而来:

http://example.com/image.jpg

像这样:

resource://addon_name/data/%22http://example.com/image.jpg%22

我不知道我是否违反了安全策略。

在我的附加脚本 (index.js) 中,我使用 sdk/request API 检索图像 URL,并将它们传递到我的内容脚本 (data/my-panel.js)。我的 data/my-panel.js 文件正在我的面板文件 (data/popup.html) 中创建 DOM 元素——包括图像——使用从 index.js 传递的 URL。以下是相关的代码:

index.js

var Request = require("sdk/request").Request;
var panel = require("sdk/panel").Panel({
  width: 500,
  height: 500,
  contentURL: "./popup.html",
  contentScriptFile: "./my-panel.js"
});
  Request({
      url: url,
      onComplete: function(response) {
           // Get the JSON data.
          json = response.json;
          // Launch the popup/panel.
          panel.show();
          panel.port.emit("sendJSON", json);
      }
  }).get();

data/my-panel.js

var title;
var desc;
var list;
var titleTextNode;   
var descTextNode; 

self.port.on("sendJSON", function(json) {

    json.docs.forEach(function(items) {
        title = JSON.stringify(items.sourceResource.title);
        desc = JSON.stringify(items.sourceResource.description);
        img = JSON.stringify(items.object);       
        console.log(img);
        var node = document.createElement("li");                 // Create a <li> node
        var imgTag = document.createElement("img");                 // Create a <img> node
        imgTag.setAttribute('src', img);
        imgTag.setAttribute('alt', desc);
        imgTag.style.width= '25px';
        titleTextNode = document.createTextNode(title);
        descTextNode = document.createTextNode(desc);
        node.appendChild(titleTextNode);                         // Append the text to <li>
        node.appendChild(descTextNode);                          // Append the text to <li>
        document.getElementById("myList").appendChild(node);     // Append <li> to <ul> with id="myList"
        document.getElementById("myImgs").appendChild(imgTag);
    });
});

console.log(img) 行正确显示了 URL,但不是在 popup.html 中...

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>
    <ul id="myList"></ul>
    <p id="myImgs"></p>
</body>
</html>

如何使图像的 src 属性直接指向远程 URL? 谢谢!

最佳答案

我发现我做错了什么。在 img URL 上使用 JSON.stringify() 是在其周围添加双引号。删除它修复了图像:

img = items.object;

关于javascript - 在 firefox 附加面板中显示远程图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36000110/

相关文章:

firefox - 像 "about:whatever"这样在 Firefox 中显示任意数据的功能

firefox - 为什么某些 CSS 不适用于 Firefox?

jquery - .attr ("disabled"、 "disabled")问题

XUL 中的 CSS 特性

javascript - 火狐扩展。如何从控制台访问 "browser"命名空间?

javascript - 使用正则表达式提取句子中的最后几个单词?

javascript - JustGage 使用 jquery 刷新

javascript - 如何在不需要时停止函数多次运行?

javascript - 火狐插件 tabs.activeTab 未定义

asp.net - 通过javascript禁用文本框中的退格键