我在Firefox扩展程序中使用了一些代码来显示用户的书签,但它不起作用(页面上显示的文字“某些文字”而不是书签),有人知道为什么吗?
代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Page displayed when a user opens a new tab or window</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function init() {
var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
.getService(Components.interfaces.nsINavHistoryService);
var options = historyService.getNewQueryOptions();
var query = historyService.getNewQuery();
var bookmarksService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
.getService(Components.interfaces.nsINavBookmarksService);
var toolbarFolder = bookmarksService.toolbarFolder;
query.setFolders([toolbarFolder], 1);
var result = historyService.executeQuery(query, options);
var rootNode = result.root;
rootNode.containerOpen = true;
// iterate over the immediate children of this folder and dump to console
output.innerHTML+='test';
for (var i = 0; i < rootNode.childCount; i ++) {
var node = rootNode.getChild(i);
output.innerHTML+='test'; // not working
output.innerHTML+=("Child: " + node.title + "\n"); //not working
//dump("Child: " + node.title + "\n"); not working
}
// close a container after using it!
rootNode.containerOpen = false
}
</script>
</head>
<body onload="init()">
<p>Some text<p>
<div id="output">
</div>
</body>
</html>
答案 0 :(得分:0)
将代码添加到覆盖文件中,它可以正常工作! (查看整个扩展程序Firefox extension not working)
代码:
function Read(file)
{
var ioService=Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var scriptableStream=Components
.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var channel=ioService.newChannel(file,null,null);
var input=channel.open();
scriptableStream.init(input);
var str=scriptableStream.read(input.available());
scriptableStream.close();
input.close();
return str;
}
gBrowser.addEventListener("DOMContentLoaded", function(e) {
var documentElement = e.originalTarget.defaultView.document;
var div = documentElement.createElement("div");
div.innerHTML = Read("chrome://firefox_extension/content/locale.html");
var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
.getService(Components.interfaces.nsINavHistoryService);
var options = historyService.getNewQueryOptions();
var query = historyService.getNewQuery();
var bookmarksService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
.getService(Components.interfaces.nsINavBookmarksService);
var toolbarFolder = bookmarksService.toolbarFolder;
query.setFolders([toolbarFolder], 1);
var result = historyService.executeQuery(query, options);
var rootNode = result.root;
rootNode.containerOpen = true;
// iterate over the immediate children of this folder and dump to console
for (var i = 0; i < rootNode.childCount; i ++) {
var node = rootNode.getChild(i);
var $i = documentElement.createElement("div");
$i.innerHTML+=("Child: " + node.title + "\n");
documentElement.body.appendChild($i);
}
// close a container after using it!
rootNode.containerOpen = false;
documentElement.body.appendChild(div);
},
false
);