remove in_module mechanism
[conkeror.git] / modules / page-modes / google-images.js
blob2ba49e7145c354602f78175fd26901537cb44861
1 /**
2  * (C) Copyright 2009 John J. Foerch
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 /**
9  * Google Images Mode
10  *
11  *   Provides browser-object overrides for various commands at
12  * google-images, to follow directly to the page that contains an
13  * image, or directly save the full-size image.  Never see that
14  * annoying frameset page from google again!
15  */
17 define_variable('google_images_imgrefurl_commands',
18                 ["follow", "follow-new-buffer",
19                  "follow-new-buffer-background",
20                  "follow-new-window", "copy"],
21     "List of commands for which the google-images-imgrefurl browser object "+
22     "should be used in Google Images Mode");
24 define_variable('google_images_imgurl_commands',
25                 ["save", "shell-command-on-file"],
26     "List of commands for which the google-images-imgurl browser object "+
27     "should be used in Google Images Mode");
29 function google_images_get_image_uri (I, prompt) {
30     var result = yield I.buffer.window.minibuffer.read_hinted_element(
31         $buffer = I.buffer,
32         $prompt = prompt,
33         $hint_xpath_expression = "//a[img]");
34     if (result instanceof Ci.nsIDOMHTMLElement) {
35         var u = Cc["@mozilla.org/network/standard-url;1"]
36             .createInstance(Ci.nsIURL);
37         u.spec = result.href;
38         yield co_return(u);
39     } else
40         yield co_return(result);
43 define_browser_object_class("google-images-imgrefurl", null,
44     function (I, prompt) {
45         var u = yield google_images_get_image_uri(I, prompt);
46         if (u instanceof Ci.nsIURL) {
47             var imgrefurl = unescape(u.query.match(/&imgrefurl=([^&]*)/)[1]);
48             yield co_return(imgrefurl);
49         } else
50             yield co_return(u);
51     });
53 define_browser_object_class("google-images-imgurl", null,
54     function (I, prompt) {
55         var u = yield google_images_get_image_uri(I, prompt);
56         if (u instanceof Ci.nsIURL) {
57             var imgurl = unescape(u.query.match(/imgurl=([^&]*)/)[1]);
58             yield co_return(imgurl);
59         } else
60             yield co_return(u);
61     });
63 define_page_mode("google-images-mode",
64     build_url_regexp($domain = /(.*\.)?google/, $path = "images"),
65     function enable (buffer) {
66         for each (var c in google_images_imgrefurl_commands) {
67             buffer.default_browser_object_classes[c] =
68                 browser_object_google_images_imgrefurl;
69         }
70         for each (var c in google_images_imgurl_commands) {
71             buffer.default_browser_object_classes[c] =
72                 browser_object_google_images_imgurl;
73         }
74     },
75     function disable (buffer) {},
76     $display_name = "Google Images");
78 page_mode_activate(google_images_mode);
80 provide("google-images");