| 1 | /* |
|---|
| 2 | * LICENSE |
|---|
| 3 | * New BSD License: http://opensource.org/licenses/bsd-license.php |
|---|
| 4 | * |
|---|
| 5 | * Copyright (c) 2008-2009, janus_wel<janus.wel.3@gmailcom> |
|---|
| 6 | * All rights reserved. |
|---|
| 7 | * |
|---|
| 8 | * Redistribution and use in source and binary forms, with or without |
|---|
| 9 | * modification, are permitted provided that the following conditions are |
|---|
| 10 | * met: |
|---|
| 11 | * |
|---|
| 12 | * * Redistributions of source code must retain the above copyright |
|---|
| 13 | * notice, this list of conditions and the following disclaimer. |
|---|
| 14 | * * Redistributions in binary form must reproduce the above copyright |
|---|
| 15 | * notice, this list of conditions and the following disclaimer in |
|---|
| 16 | * the documentation and/or other materials provided with the |
|---|
| 17 | * distribution. |
|---|
| 18 | * * Neither the name of the <ORGANIZATION> nor the names of its |
|---|
| 19 | * contributors may be used to endorse or promote products derived |
|---|
| 20 | * from this software without specific prior written permission. |
|---|
| 21 | * |
|---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|---|
| 23 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|---|
| 24 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|---|
| 25 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
|---|
| 26 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|---|
| 27 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|---|
| 28 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 29 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|---|
| 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 32 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 33 | * */ |
|---|
| 34 | |
|---|
| 35 | let PLUGIN_INFO = |
|---|
| 36 | <VimperatorPlugin> |
|---|
| 37 | <name>{NAME}</name> |
|---|
| 38 | <description>extend image operation.</description> |
|---|
| 39 | <description lang="ja">画像操作特集。</description> |
|---|
| 40 | <author mail="janus_wel@fb3.so-net.ne.jp" homepage="http://d.hatena.ne.jp/janus_wel">janus_wel</author> |
|---|
| 41 | <license document="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license> |
|---|
| 42 | <version>0.20</version> |
|---|
| 43 | <minversion>2.0</minversion> |
|---|
| 44 | <maxversion>2.1pre</maxversion> |
|---|
| 45 | <detail><![CDATA[ |
|---|
| 46 | == USAGE == |
|---|
| 47 | Extended-hints mode ';m' to yank image URL and ';M' to save image are |
|---|
| 48 | available. These are default settings. You can change these by below |
|---|
| 49 | settings. |
|---|
| 50 | |
|---|
| 51 | Ex-command ':downimageall' is also available. This is to download all |
|---|
| 52 | images of current page, but it effects heavy load to the server, you must |
|---|
| 53 | use carefully. We are NOT RESPONSIBLE for result of this command. |
|---|
| 54 | |
|---|
| 55 | Additionally, following modes are available if you installed Image Zoom |
|---|
| 56 | add-on ( https://addons.mozilla.org/firefox/addon/139 ). These feature |
|---|
| 57 | were provided by Frank Blendinger. Thanks !! |
|---|
| 58 | |
|---|
| 59 | - ';i' to zoom in image |
|---|
| 60 | - ';x' to zoom out image |
|---|
| 61 | - ';X' to zoom reset image |
|---|
| 62 | - ';z' to zoom fit image |
|---|
| 63 | - ';Z' to custom zoom image |
|---|
| 64 | |
|---|
| 65 | == SETTING == |
|---|
| 66 | :image_extender_yank_key: |
|---|
| 67 | The key to yank image URL(default: 'm'). |
|---|
| 68 | :image_extender_save_key: |
|---|
| 69 | The key to save image(default: 'M'). |
|---|
| 70 | :image_extender_zoom_in_key: |
|---|
| 71 | The key to zoom in image(default: 'i'). |
|---|
| 72 | :image_extender_zoom_out_key: |
|---|
| 73 | The key to zoom out image(default: 'x'). |
|---|
| 74 | :image_extender_zoom_reset_key: |
|---|
| 75 | The key to zoom reset image(default: 'X'). |
|---|
| 76 | :image_extender_zoom_fit_key: |
|---|
| 77 | The key to zoom fit image(default: 'z'). |
|---|
| 78 | :image_extender_zoom_custom_key: |
|---|
| 79 | The key to custom zoom image(default: 'Z'). |
|---|
| 80 | :image_extender_skip_prompt: |
|---|
| 81 | If this setted 'true', skip prompt to locate and name. Changing this value |
|---|
| 82 | will reflect dynamically. ':downimageall' command ignore this |
|---|
| 83 | setting(default: 'false'). |
|---|
| 84 | |
|---|
| 85 | == EXAMPLE == |
|---|
| 86 | In .vimperatorrc |
|---|
| 87 | |
|---|
| 88 | >|| |
|---|
| 89 | image_extender_yank_key='g' |
|---|
| 90 | image_extender_save_key='e' |
|---|
| 91 | image_skip_prompt='true' |
|---|
| 92 | ||< |
|---|
| 93 | |
|---|
| 94 | In this settings, ';g' start extended-hints mode to yank image URL. ';e' |
|---|
| 95 | start it to save image, and prompt is not displayed at save operation. |
|---|
| 96 | ]]></detail> |
|---|
| 97 | <detail lang="ja"><![CDATA[ |
|---|
| 98 | == USAGE == |
|---|
| 99 | 画像の URL をヤンクする ';m' と 画像を保存する ';M' という拡張ヒントモードが使えるようになります。 |
|---|
| 100 | これらはデフォルト設定です。後述する設定でキーを変更できます。 |
|---|
| 101 | |
|---|
| 102 | ':downimageall' という ex コマンドも使えるようになります。 |
|---|
| 103 | これは現在のページの画像をすべて保存するものですが、サーバに大きな負荷がかかるため注意して使ってください。 |
|---|
| 104 | このコマンドの使用による結果は一切の責任を負いかねます。 |
|---|
| 105 | |
|---|
| 106 | さらに、 Image Zoom ( https://addons.mozilla.org/firefox/addon/139 ) アドオンをインストールしている場合以下のモードが使えるようになります。 |
|---|
| 107 | この機能は Frank Blendinger さんによって提供されました。ありがとう !! |
|---|
| 108 | |
|---|
| 109 | - 画像を拡大する ';i' |
|---|
| 110 | - 画像を縮小する ';x' |
|---|
| 111 | - 画像の拡大縮小を元に戻す ';X' |
|---|
| 112 | - 画像を画面にあわせて表示する ';z' |
|---|
| 113 | - 画像をカスタムズームさせる ';Z' |
|---|
| 114 | |
|---|
| 115 | == SETTING == |
|---|
| 116 | :image_extender_yank_key: |
|---|
| 117 | 画像 の URL をヤンクするキーです (デフォルト: 'm') 。 |
|---|
| 118 | :image_extender_save_key: |
|---|
| 119 | 画像を保存するキーです (デフォルト: 'M') 。 |
|---|
| 120 | :image_extender_zoom_in_key: |
|---|
| 121 | 画像を拡大するキーです (デフォルト: 'i') 。 |
|---|
| 122 | :image_extender_zoom_out_key: |
|---|
| 123 | 画像を縮小するキーです (デフォルト: 'x') 。 |
|---|
| 124 | :image_extender_zoom_reset_key: |
|---|
| 125 | 画像の拡大縮小を元に戻すキーです (デフォルト: 'X') 。 |
|---|
| 126 | :image_extender_zoom_fit_key: |
|---|
| 127 | 画像を画面にあわせて拡大縮小するキーです (デフォルト: 'z') 。 |
|---|
| 128 | :image_extender_zoom_custom_key: |
|---|
| 129 | 画像をカスタムズームさせるキーです (デフォルト: 'Z') 。 |
|---|
| 130 | :image_extender_skip_prompt: |
|---|
| 131 | この値が 'true' の場合、保存する場所や名前を指定するダイアログは表示されません。 |
|---|
| 132 | この値の変更は即座に反映されます。 |
|---|
| 133 | ':downimageall' コマンドはこの設定を無視します (デフォルト: 'false') 。 |
|---|
| 134 | |
|---|
| 135 | == EXAMPLE == |
|---|
| 136 | .vimperatorrc の中で、 |
|---|
| 137 | |
|---|
| 138 | >|| |
|---|
| 139 | image_extender_yank_key='g' |
|---|
| 140 | image_extender_save_key='e' |
|---|
| 141 | image_skip_prompt='true' |
|---|
| 142 | ||< |
|---|
| 143 | |
|---|
| 144 | と設定すると、 ';g' で画像の URL をヤンクする拡張ヒントモードが開始されます。 |
|---|
| 145 | ';e' で画像を保存しますが、その際ダイアログは表示されません。 |
|---|
| 146 | ]]></detail> |
|---|
| 147 | </VimperatorPlugin>; |
|---|
| 148 | |
|---|
| 149 | ( function () { |
|---|
| 150 | |
|---|
| 151 | // default settings |
|---|
| 152 | const yankKey = liberator.globalVariables.image_extender_yank_key || 'm'; |
|---|
| 153 | const saveKey = liberator.globalVariables.image_extender_save_key || 'M'; |
|---|
| 154 | const zoomInKey = liberator.globalVariables.image_extender_zoom_in_key || 'i'; |
|---|
| 155 | const zoomOutKey = liberator.globalVariables.image_extender_zoom_out_key || 'x'; |
|---|
| 156 | const zoomResetKey = liberator.globalVariables.image_extender_zoom_reset_key || 'X'; |
|---|
| 157 | const zoomFitKey = liberator.globalVariables.image_extender_zoom_fit_key || 'z'; |
|---|
| 158 | const zoomCustomKey = liberator.globalVariables.image_extender_zoom_custom_key || 'Z'; |
|---|
| 159 | |
|---|
| 160 | // common settings |
|---|
| 161 | const query = '//img[@src and not(starts-with(@src, "data:"))]'; |
|---|
| 162 | const interval = 200; // 5 images per second |
|---|
| 163 | |
|---|
| 164 | // extended-hints mode |
|---|
| 165 | // to yank image URL |
|---|
| 166 | hints.addMode( |
|---|
| 167 | yankKey, |
|---|
| 168 | 'Yank image URL', |
|---|
| 169 | function (element) util.copyToClipboard(element.src, true), |
|---|
| 170 | function () query |
|---|
| 171 | ); |
|---|
| 172 | // to save image |
|---|
| 173 | hints.addMode( |
|---|
| 174 | saveKey, |
|---|
| 175 | 'Save image', |
|---|
| 176 | function (element) { |
|---|
| 177 | let skipPrompt = stringToBoolean(liberator.globalVariables.image_extender_skip_prompt, false); |
|---|
| 178 | |
|---|
| 179 | try { saveImage(element, skipPrompt); } |
|---|
| 180 | catch (e) { liberator.echoerr(e); } |
|---|
| 181 | }, |
|---|
| 182 | function () query |
|---|
| 183 | ); |
|---|
| 184 | if (window.izImage) { |
|---|
| 185 | // to zoom in image |
|---|
| 186 | hints.addMode( |
|---|
| 187 | zoomInKey, |
|---|
| 188 | 'Zoom in image', |
|---|
| 189 | function (element) { |
|---|
| 190 | var oizImage = new izImage(element); |
|---|
| 191 | oizImage.zoom(nsIPrefBranchObj.getIntPref("zoomvalue")/100); |
|---|
| 192 | reportStatus(oizImage); |
|---|
| 193 | }, |
|---|
| 194 | function () query |
|---|
| 195 | ); |
|---|
| 196 | // to zoom out image |
|---|
| 197 | hints.addMode( |
|---|
| 198 | zoomOutKey, |
|---|
| 199 | 'Zoom out image', |
|---|
| 200 | function (element) { |
|---|
| 201 | var oizImage = new izImage(element); |
|---|
| 202 | oizImage.zoom(100/nsIPrefBranchObj.getIntPref("zoomvalue")); |
|---|
| 203 | reportStatus(oizImage); |
|---|
| 204 | }, |
|---|
| 205 | function () query |
|---|
| 206 | ); |
|---|
| 207 | // to zoom reset image |
|---|
| 208 | hints.addMode( |
|---|
| 209 | zoomResetKey, |
|---|
| 210 | 'Zoom reset image', |
|---|
| 211 | function (element) { |
|---|
| 212 | var oizImage = new izImage(element); |
|---|
| 213 | oizImage.setZoom(100); |
|---|
| 214 | reportStatus(oizImage); |
|---|
| 215 | }, |
|---|
| 216 | function () query |
|---|
| 217 | ); |
|---|
| 218 | // to zoom fit image |
|---|
| 219 | hints.addMode( |
|---|
| 220 | zoomFitKey, |
|---|
| 221 | 'Zoom fit image', |
|---|
| 222 | function (element) { |
|---|
| 223 | var oizImage = new izImage(element); |
|---|
| 224 | oizImage.fit(nsIPrefBranchObj.getBoolPref("autocenter")); |
|---|
| 225 | reportStatus(oizImage); |
|---|
| 226 | }, |
|---|
| 227 | function () query |
|---|
| 228 | ); |
|---|
| 229 | // to custom zoom image |
|---|
| 230 | hints.addMode( |
|---|
| 231 | zoomCustomKey, |
|---|
| 232 | 'Custom zoom image', |
|---|
| 233 | function (element) { |
|---|
| 234 | var oizImage = new izImage(element); |
|---|
| 235 | openDialog("chrome://imagezoom/content/customzoom.xul", "", "chrome,modal,centerscreen", "Image", oizImage); |
|---|
| 236 | reportStatus(oizImage); |
|---|
| 237 | }, |
|---|
| 238 | function () query |
|---|
| 239 | ); |
|---|
| 240 | } |
|---|
| 241 | commands.addUserCommand( |
|---|
| 242 | ['downimageall'], |
|---|
| 243 | 'download all images of current page', |
|---|
| 244 | function () { |
|---|
| 245 | // refer: http://d.hatena.ne.jp/amachang/20071108/1194501306 |
|---|
| 246 | let images = buffer.evaluateXPath(query); |
|---|
| 247 | let l = images.snapshotLength; |
|---|
| 248 | let i = 0; |
|---|
| 249 | setTimeout ( function a() { |
|---|
| 250 | if (!(i < l)) return; |
|---|
| 251 | try { saveImage(images.snapshotItem(i), true); } |
|---|
| 252 | catch (e) { liberator.echoerr(e); } |
|---|
| 253 | ++i; |
|---|
| 254 | setTimeout(a, interval); |
|---|
| 255 | }, interval); |
|---|
| 256 | }, |
|---|
| 257 | {} |
|---|
| 258 | ); |
|---|
| 259 | |
|---|
| 260 | // stuff function |
|---|
| 261 | function stringToBoolean(str, defaultValue) { |
|---|
| 262 | return !str ? (defaultValue ? true : false) |
|---|
| 263 | : str.toLowerCase() === 'false' ? false |
|---|
| 264 | : /^\d+$/.test(str) ? (parseInt(str) ? true : false) |
|---|
| 265 | : true; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | function saveImage(imgElement, skipPrompt) { |
|---|
| 269 | let doc = imgElement.ownerDocument; |
|---|
| 270 | let url = imgElement.src; |
|---|
| 271 | let filename = url.split(/\/+/g).pop(); |
|---|
| 272 | |
|---|
| 273 | urlSecurityCheck(url, doc.nodePrincipal); |
|---|
| 274 | // we always want to save that link relative to the current working directory |
|---|
| 275 | options.setPref("browser.download.lastDir", io.getCurrentDirectory().path); |
|---|
| 276 | saveImageURL(url, filename, null, true, skipPrompt, makeURI(url, doc.characterSet)); |
|---|
| 277 | } |
|---|
| 278 | } )(); |
|---|
| 279 | |
|---|
| 280 | // vim: sw=4 sts=4 ts=4 et |
|---|