在Firefox中使用超级Bookmarklet

首先要说的是,本文是给那些比较"发烧"的火狐玩家看的,其他浏览器用户可能对本文无爱.

很多人喜欢使用bookmarklet,放到自己的书签栏里,在点击时对当前页面进行各种各样的操作,比如原文翻译,比如短网址化,比如"分享到","收藏到",等等.和GreaseMonkey脚本的区别就是,这些操作不需要在每次页面加载后都执行,只需要在自己偶尔需要的时候点击执行就可以.

bookmarklet是一段由"javascript:"伪协议开头的JavaScript代码,这段代码执行的上下文是当前标签页中的页面窗口(window).比如"javascript:alert(location)",会弹出类似"http://www.cnblogs.com/ziyunfei"这样的URL.

我写了一个工具,可以让书签栏中以"chromejs:"伪协议开头的JavaScript代码执行在当前浏览器窗口下(chromeWindow),这样,执行"chromejs:alert(location)",会弹出"chrome://browser/content/browser.xul",这个XUL页面就是Firefox的主界面.你需要先安装我写的这个工具,再尝试下面的这些例子.

扩展:https://files.cnblogs.com/ziyunfei/SuperBookmarklet.xpi

或者

UC脚本:https://files.cnblogs.com/ziyunfei/SuperBookmarklet.uc.js

在浏览器上下文中执行的JavaScript代码有特殊权限,你可以把你不常用的鼠标手势代码做成这样的超级书签放在书签栏里.

下面举几个例子,其中的链接都是用的"chromejs"伪协议,你可以直接拖到书签栏里.

比如:重启Firefox

Application.restart()

比如:当前页面截图

(function () {
    var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
    canvas.width = content.document.documentElement.scrollWidth;
    canvas.height = content.document.documentElement.scrollHeight;
    var ctx = canvas.getContext("2d");
    ctx.drawWindow(content, 0, 0, canvas.width, canvas.height, "rgb(255,255,255)");
    canvas.toBlob(function (blob, blobURL) {
        saveImageURL(blobURL = URL.createObjectURL(blob), content.document.title + ".png", null, null, true, null, document);
        URL.revokeObjectURL(blobURL)
    })
})()

比如:打开资源管理器

(function () {
    var file = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties).get("WinD", Components.interfaces.nsILocalFile);
    file.append("explorer.exe");
    var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess);
    process.init(file);
    process.run(false, [","], 1);
})()

比如:打开光驱(windows)

Components.utils.import("resource://gre/modules/ctypes.jsm");
ctypes.open("winmm.dll").declare("mciSendStringW", ctypes.default_abi || ctypes.winapi_abi, ctypes.uint32_t, ctypes.jschar.ptr, ctypes.uint32_t, ctypes.unsigned_int, ctypes.uint32_t)("set cdaudio door open", 0, 0, 0);

这就是超级书签,是不是既强大又方便.

posted @ 2013-01-26 16:48  紫云飞  阅读(3694)  评论(4编辑  收藏  举报