forked from aminomancer/uc.css.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchSelectionShortcut.uc.js
135 lines (130 loc) · 6.25 KB
/
searchSelectionShortcut.uc.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// ==UserScript==
// @name Search Selection Keyboard Shortcut
// @version 1.2
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description Adds a new keyboard shortcut (ctrl+shift+F) that searches your default search engine for whatever text you currently have highlighted. This does basically the same thing as the context menu option "Search {Engine} for {Selection}" except that if you highlight a URL, instead of searching for the selection it will navigate directly to the URL.
// ==/UserScript==
(() => {
function frameScript() {
const utils = {};
const { Services: Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils: XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(utils, {
E10SUtils: "resource://gre/modules/E10SUtils.jsm",
SelectionUtils: "resource://gre/modules/SelectionUtils.jsm",
});
let attachKeyListener = (d, n) => {
if ("document-element-inserted" == n && content && d == content.document)
try {
content.addEventListener("keydown", (e) => {
if ("KeyF" === e.code && e.ctrlKey && e.shiftKey && !e.repeat) {
try {
let s = utils.SelectionUtils.getSelectionDetails(content);
if (s.text && !s.docSelectionIsCollapsed) {
let msg = {
csp: utils.E10SUtils.serializeCSP(
e.composedTarget.ownerDocument.csp
),
text: s.text,
linkURL: s.linkURL,
locationURL: content.location.href,
};
try {
sendAsyncMessage("ctrl-shift-f", msg);
} catch (e) {}
}
} catch (e) {}
e.stopPropagation(), e.stopImmediatePropagation(), e.preventDefault();
}
});
} catch (e) {}
};
Services.obs.addObserver(attachKeyListener, "document-element-inserted");
attachKeyListener(content.document, "document-element-inserted");
}
function init() {
try {
messageManager.loadFrameScript(
"data:application/javascript," +
encodeURIComponent(`(${frameScript.toString()})()`),
true
);
} catch (e) {}
messageManager.addMessageListener("ctrl-shift-f", (message) => {
if (message.target === gBrowser.selectedBrowser) {
try {
let csp = E10SUtils.deserializeCSP(message.data.csp);
let { text, linkURL, locationURL } = message.data;
let principal = gBrowser.selectedBrowser.contentPrincipal;
let options = {
inBackground: false,
triggeringPrincipal: principal,
relatedToCurrent: true,
};
let where = new RegExp(
`(${BROWSER_NEW_TAB_URL}|${HomePage.get(window)})`,
"i"
).test(locationURL)
? "current"
: "tab";
if (/^((chrome|resource|file|moz-extension)\:\/\/|about:).+/.test(text)) {
if (/^moz-extension\:\/\/.+/.test(text)) {
let host = Services.io.newURI(text)?.host;
let policy = WebExtensionPolicy.getByHostname(host);
let extPrincipal = policy && policy.extension.principal;
if (extPrincipal) {
options.triggeringPrincipal = extPrincipal;
return openLinkIn(text, where, options);
}
} else {
options.triggeringPrincipal =
Services.scriptSecurityManager.getSystemPrincipal();
return openLinkIn(text, where, options);
}
} else if (linkURL) {
let fixup, fixable;
try {
fixup = Services.uriFixup.getFixupURIInfo(
text,
Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP
);
fixable = true;
} catch (e) {
fixable = false;
}
if (fixable && !fixup._keywordProviderName) {
linkURL =
fixup._fixedURI.scheme === "http"
? fixup._fixedURI.host
: fixup._fixedURI.spec;
if (linkURL) return openLinkIn(linkURL, where, options);
}
}
BrowserSearch._loadSearch(
text,
where,
false,
"contextmenu",
principal,
csp,
false
);
} catch (e) {}
}
});
}
if (gBrowserInit.delayedStartupFinished) {
setTimeout(init, 1000);
} else {
let delayedStartupFinished = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedStartupFinished, topic);
setTimeout(init, 1000);
}
};
Services.obs.addObserver(delayedStartupFinished, "browser-delayed-startup-finished");
}
})();