File: google-images.js

package info (click to toggle)
conkeror 1.0.3%2Bgit170123-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,988 kB
  • sloc: ansic: 280; sh: 255; xml: 173; makefile: 69
file content (87 lines) | stat: -rw-r--r-- 3,062 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * (C) Copyright 2009 John J. Foerch
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

/**
 * Google Images Mode
 *
 *   Provides browser-object overrides for various commands at
 * google-images, to follow directly to the page that contains an
 * image, or directly save the full-size image.  Never see that
 * annoying frameset page from google again!
 */

define_variable('google_images_imgrefurl_commands',
                ["follow", "follow-new-buffer",
                 "follow-new-buffer-background",
                 "follow-new-window", "copy"],
    "List of commands for which the google-images-imgrefurl browser object "+
    "should be used in Google Images Mode");

define_variable('google_images_imgurl_commands',
                ["save", "shell-command-on-file"],
    "List of commands for which the google-images-imgurl browser object "+
    "should be used in Google Images Mode");

function google_images_get_image_uri (I, prompt) {
    var result = yield I.buffer.window.minibuffer.read_hinted_element(
        $buffer = I.buffer,
        $prompt = prompt,
        $hint_xpath_expression = "//a[img]");
    if (result instanceof Ci.nsIDOMHTMLElement) {
        var u = Cc["@mozilla.org/network/standard-url;1"]
            .createInstance(Ci.nsIURL);
        u.spec = result.href;
        yield co_return(u);
    } else
        yield co_return(result);
}

define_browser_object_class("google-images-imgrefurl", null,
    function (I, prompt) {
        var u = yield google_images_get_image_uri(I, prompt);
        if (u instanceof Ci.nsIURL) {
            var imgrefurl = unescape(u.query.match(/&imgrefurl=([^&]*)/)[1]);
            yield co_return(imgrefurl);
        } else
            yield co_return(u);
    });

define_browser_object_class("google-images-imgurl", null,
    function (I, prompt) {
        var u = yield google_images_get_image_uri(I, prompt);
        if (u instanceof Ci.nsIURL) {
            var imgurl = unescape(u.query.match(/imgurl=([^&]*)/)[1]);
            yield co_return(imgurl);
        } else
            yield co_return(u);
    });

define_page_mode("google-images-mode",
    build_url_regexp($domain = /(.*\.)?google/, $path = "images"),
    function enable (buffer) {
        for each (var c in google_images_imgrefurl_commands) {
            buffer.default_browser_object_classes[c] =
                browser_object_google_images_imgrefurl;
        }
        for each (var c in google_images_imgurl_commands) {
            buffer.default_browser_object_classes[c] =
                browser_object_google_images_imgurl;
        }
    },
    function disable (buffer) {
        for each (var c in google_images_imgrefurl_commands) {
            delete buffer.default_browser_object_classes[c];
        }
        for each (var c in google_images_imgurl_commands) {
            delete buffer.default_browser_object_classes[c];
        }
    },
    $display_name = "Google Images");

page_mode_activate(google_images_mode);

provide("google-images");