| 1 | /** |
|---|
| 2 | * bookmarklet wo command ni suru plugin |
|---|
| 3 | * |
|---|
| 4 | * @author halt feits <halt.feits@gmail.com> |
|---|
| 5 | * @version 0.6.4 |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | let PLUGIN_INFO = |
|---|
| 9 | <VimperatorPlugin> |
|---|
| 10 | <name>{NAME}</name> |
|---|
| 11 | <description>convert bookmarklets to commands</description> |
|---|
| 12 | <description lang="ja">ブックマークレットをコマンドにする</description> |
|---|
| 13 | <author mail="halt.feits@gmail.com">halt feits</author> |
|---|
| 14 | <version>0.6.6</version> |
|---|
| 15 | <minVersion>2.0pre</minVersion> |
|---|
| 16 | <maxVersion>2.1pre</maxVersion> |
|---|
| 17 | <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/commandBookmarklet.js</updateURL> |
|---|
| 18 | <detail><![CDATA[ |
|---|
| 19 | == SYNOPSIS == |
|---|
| 20 | This plugin automatically converts bookmarklets to valid commands for Vimperator. |
|---|
| 21 | |
|---|
| 22 | == COMMAND == |
|---|
| 23 | Nothing built-in command, but each bookmarklets convert to commands that start with "bml". |
|---|
| 24 | |
|---|
| 25 | == EXAMPLE == |
|---|
| 26 | "Hatena-Bookmark" -> bmlhatena-bookmark |
|---|
| 27 | |
|---|
| 28 | == GLOBAL VARIABLES == |
|---|
| 29 | command_bookmarklet_prefix: |
|---|
| 30 | This variable determines the prefix of a command name. |
|---|
| 31 | command_bookmarklet_use_sandbox: |
|---|
| 32 | When this variable is 1, execute the script of bookmarklets in Sandbox. |
|---|
| 33 | If you use NoScript addon, probably you should enable this option. |
|---|
| 34 | |
|---|
| 35 | == KNOWN ISSUES == |
|---|
| 36 | When title has non-ASCII characters, it converts to unaccountable command. |
|---|
| 37 | You should rewrite title of bookmarklet to ASCII characters, to escape this issue. |
|---|
| 38 | |
|---|
| 39 | ]]></detail> |
|---|
| 40 | <detail lang="ja"><![CDATA[ |
|---|
| 41 | == SYNOPSIS == |
|---|
| 42 | このプラグインはブックマークレットを Vimperator で実行可能なコマンドに自動的に変換します。 |
|---|
| 43 | |
|---|
| 44 | == COMMAND == |
|---|
| 45 | 固有のコマンドはありませんが、それぞれのブックマークレットは "bml" ではじまるコマンドに変換されます。 |
|---|
| 46 | |
|---|
| 47 | == EXAMPLE == |
|---|
| 48 | "Hatena-Bookmark" -> bmlhatena-bookmark |
|---|
| 49 | |
|---|
| 50 | == GLOBAL VARIABLES == |
|---|
| 51 | command_bookmarklet_prefix: |
|---|
| 52 | コマンドの先頭に付加される文字列を規定します。 |
|---|
| 53 | デフォルトは "bml" |
|---|
| 54 | command_bookmarklet_use_sandbox: |
|---|
| 55 | 1 の時、ブックマークレットのスクリプトを sandbox で実行します。 |
|---|
| 56 | NoScript アドオンをつかっている場合は、このオプションを有効にする必要があるでしょう。 |
|---|
| 57 | |
|---|
| 58 | == KNOWN ISSUES == |
|---|
| 59 | タイトルに ASCII 文字以外が含まれている場合、わけのわからないコマンドになります。 |
|---|
| 60 | この問題を避けるためにブックマークレットのタイトルを ASCII 文字のみに書き換えることをおすすめします。 |
|---|
| 61 | |
|---|
| 62 | ]]></detail> |
|---|
| 63 | </VimperatorPlugin>; |
|---|
| 64 | |
|---|
| 65 | ( function () { |
|---|
| 66 | |
|---|
| 67 | let prefix = liberator.globalVariables.command_bookmarklet_prefix; |
|---|
| 68 | if (prefix === undefined) |
|---|
| 69 | prefix = 'bml'; |
|---|
| 70 | |
|---|
| 71 | let items = bookmarks.get('javascript:'); |
|---|
| 72 | if (!items.length) { |
|---|
| 73 | liberator.echoerr('No bookmarks set'); |
|---|
| 74 | return; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | items.forEach(function (item) { |
|---|
| 78 | commands.addUserCommand( |
|---|
| 79 | [toValidCommandName(item.title)], |
|---|
| 80 | 'bookmarklet : ' + item.title, |
|---|
| 81 | function () evalScript(item.url), |
|---|
| 82 | { shortHelp: 'Bookmarklet' }, |
|---|
| 83 | false |
|---|
| 84 | ); |
|---|
| 85 | }); |
|---|
| 86 | |
|---|
| 87 | function toBoolean (value, def) { |
|---|
| 88 | switch (typeof value) { |
|---|
| 89 | case 'undefined': |
|---|
| 90 | return def; |
|---|
| 91 | case 'number': |
|---|
| 92 | return !!value; |
|---|
| 93 | case 'string': |
|---|
| 94 | return !/^(\d+|false)$/i.test(value) || parseInt(value); |
|---|
| 95 | default: |
|---|
| 96 | return !!value; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | function evalInSandbox (str) { |
|---|
| 101 | let sandbox = new Components.utils.Sandbox("about:blank"); |
|---|
| 102 | sandbox.__proto__ = content.window.wrappedJSObject; |
|---|
| 103 | return Components.utils.evalInSandbox(str, sandbox); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | function evalScript (url) { |
|---|
| 107 | if (toBoolean(liberator.globalVariables.command_bookmarklet_use_sandbox, false)) { |
|---|
| 108 | evalInSandbox(decodeURIComponent(url.replace(/^\s*javascript:/i, ''))); |
|---|
| 109 | } else { |
|---|
| 110 | liberator.open(url); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | function toValidCommandName(str) { |
|---|
| 115 | str = prefix + escape(str.replace(/ +/g, '').toLowerCase()).replace(/[^a-zA-Z]+/g,''); |
|---|
| 116 | return str.substr(0, str.length > 50 ? 50 : str.length); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | } )(); |
|---|
| 120 | // vim:sw=2 ts=2 et: |
|---|