首先要說的是,本文是給那些比較"發燒"的火狐玩家看的,其他瀏覽器用戶可能對本文無愛.
很多人喜歡使用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的主界面.你需要先安裝我寫的這個工具,再嘗試下面的這些例子.
擴展:http://files.cnblogs.com/ziyunfei/SuperBookmarklet.xpi
或者
UC腳本:http://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);
這就是超級書簽,是不是既強大又方便.