如果您的可点击小书签坏了,而您又想找回它,您可以创建一个可点击按钮,而不是使用Custom Buttons Firefox 扩展。
按钮相对于 Scratchpad 运行的优势:
- 您实际上可以保存小书签(按钮),
- 您可以拥有一个自己的漂亮图标(创建一些图像,例如 PNG 文件,将其导入并在新按钮对话框中对其进行 base64_encode)。
这个扩展有点特别,因为按钮运行在 Firefox chrome 级别,所以它们有更多特权(您可以与浏览器的 API 交互),并且没有一对一1 普通 JS 和按钮代码之间的对应关系(需要一些调整)。更准确地说,从按钮中看到的document 和window 并不是您所期望的。
但是,您可以将“好”window 和 document 分配给您的变量,然后改为处理这些变量(最好不要重新定义窗口;)
这是我编写的在 Fx10 中运行良好的示例代码:
// get proper 'window' and 'document' into our private variables
var theWindow = window.top.getBrowser().selectedBrowser.contentWindow;
var theDocument = theWindow.document;
// here we go
var input = theDocument.getElementById("foo");
input.focus(); // just to show you it's working, unnecessary in fact
// simulate keyboard event
var evt = theDocument.createEvent("KeyboardEvent");
evt.initKeyEvent ("keypress", true, true, theWindow ,
0, 0, 0, 0, 0, 65); // 65 == "A"
input.dispatchEvent(evt);
// show alert after 2 sec
theWindow.setTimeout( function(){
input.value += "B";
theWindow.alert(input.value); // alerts "AB"
},2000);
不要直接使用全局函数(如setTimeout,或alert),您必须在它们前面加上theWindow.,并将document/window替换为本地theDocument/theWindow和它似乎正在工作。我还没有对它进行彻底的测试,但是对于非常复杂的情况。
要添加按钮,右键单击您已有的任何按钮,然后选择“添加新按钮...”。