在 firefox 附加面板中显示远程图像
Displaying remote images in a firefox add-on panel
我正在尝试在 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?
谢谢!
我不太确定 SDK 权限,但作为最后的手段,您可以将远程 url 变成这样的资源 URI -
var res = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
res.setSubstitution("rawr", Services.io.newURI('http://www.bing.com/',null,null));
// now try navigating to resource://rawr it will load bing
然后您可以加载 resource://rawr
,它应该可以工作。
我发现我做错了什么。在 img URL 上使用 JSON.stringify() 是在其周围添加双引号。删除它修复了图像:
img = items.object;
我正在尝试在 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?
谢谢!
我不太确定 SDK 权限,但作为最后的手段,您可以将远程 url 变成这样的资源 URI -
var res = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
res.setSubstitution("rawr", Services.io.newURI('http://www.bing.com/',null,null));
// now try navigating to resource://rawr it will load bing
然后您可以加载 resource://rawr
,它应该可以工作。
我发现我做错了什么。在 img URL 上使用 JSON.stringify() 是在其周围添加双引号。删除它修复了图像:
img = items.object;