/
bootstrap.js
111 lines (98 loc) · 4.12 KB
/
bootstrap.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
let {UserAgentOverrides} = Cu.import("resource://gre/modules/UserAgentOverrides.jsm", {});
Cu.import('resource://gre/modules/Services.jsm');
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const BUNDLE = "chrome://reverseimagesearch/locale/reverseimagesearch.properties?" + Math.random(); // Randomize URI to work around bug 719376
XPCOMUtils.defineLazyGetter(this, "Strings", function() {
return Services.strings.createBundle(BUNDLE);
});
function isPrivateTab(tab) {
return tab.browser.docShell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing;
}
/*
Google redirects to the homepage if we attempt a reverse image search using the default UA
so spoof the chrome user agent (see bugzil.la/1200652)
*/
const CHROME_USER_AGENT = 'Mozilla/5.0 (Linux; Android) AppleWebKit (KHTML, like Gecko) Mobile';
let searcherId = null;
function loadIntoWindow(window) {
if (!window)
return;
searcherId = window.NativeWindow.contextmenus.add(
Strings.GetStringFromName("searchimage_contextmenu"),
{
matches: function(element) {
// imageLocationCopyableContext also matches data urls, which google image search does not support
return window.NativeWindow.contextmenus.imageLocationCopyableContext.matches(element) && element.currentURI.scheme.startsWith('http');
}
},
function (target) {
let imgUrl = target.src;
window.BrowserApp.addTab('https://www.google.com/searchbyimage?image_url=' + encodeURIComponent(imgUrl), {
isPrivate: isPrivateTab(window.BrowserApp.selectedTab),
parentId: window.BrowserApp.selectedTab.id
});
}
);
}
function unloadFromWindow(window) {
if (!window)
return;
window.NativeWindow.contextmenus.remove(searcherId);
}
var windowListener = {
onOpenWindow: function (aWindow) {
// Wait for the window to finish loading
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener("UIReady", function () {
domWindow.removeEventListener("UIReady", arguments.callee, false);
loadIntoWindow(domWindow);
}, false);
},
onCloseWindow: function (aWindow) {},
onWindowTitleChange: function (aWindow, aTitle) {}
};
function startup(aData, aReason) {
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
// Load into any existing windows
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
loadIntoWindow(domWindow);
}
// Load into any new windows
wm.addListener(windowListener);
/*
Reverse Image Search first hits the url of the form:
google.<COUNTRY SPECIFIC TLD>/searchbyimage?image_url=<IMAGE URL>
which then redircts to the url of the form:
google.<COUNTRY SPECIFIC TLD>/search?tbs=sbi:<...>
We need to provide the Chrome User Agent for these urls
*/
UserAgentOverrides.addComplexOverride(function googleImageSearchUserAgentOverride(channel, DEFAULT_UA){
if (channel.URI.host.indexOf("google.") !== -1 && (channel.URI.path.startsWith("/searchbyimage?") ||
channel.URI.path.startsWith("/search?tbs=sbi:") ||
channel.URI.path.indexOf("tbs=simg:") !== -1)){
return CHROME_USER_AGENT;
}
});
}
function shutdown(aData, aReason) {
// When the application is shutting down we normally don't have to clean
// up any UI changes made
if (aReason == APP_SHUTDOWN)
return;
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
// Stop listening for new windows
wm.removeListener(windowListener);
// Unload from any existing windows
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
}
}
function install(aData, aReason) {}
function uninstall(aData, aReason) {}