-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag-text-to-search.user.js
43 lines (39 loc) · 1.63 KB
/
drag-text-to-search.user.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
// ==UserScript==
// @name Drag text to search
// @version 1.1
// @description Highlight, drag, then release text to search in Google
// @author Ryan Buening
// @license MIT
// @namespace https://github.com/ryanbuening/userscripts
// @run-at document-start
// @include *
// ==/UserScript==
(function() {
const isTextArea = element => ['email', 'number', 'password', 'search', 'tel', 'text', 'url', 'textarea'].includes(element.type) && !element.disabled;
const handleDragOver = event => {
if (event.dataTransfer.types.includes('text/uri-list')) {
event.dataTransfer.dropEffect = 'link';
event.preventDefault();
} else if (event.dataTransfer.types.includes('text/plain') && !isTextArea(event.target)) {
event.dataTransfer.dropEffect = 'link';
event.preventDefault();
}
};
const handleDrop = event => {
if (event.dataTransfer.types.includes('text/uri-list')) {
const url = event.dataTransfer.getData('URL');
//console.log("url: " + url);
if (!url.includes("about:")) {
// window.open(url, '_blank');
// event.preventDefault();
}
} else if (event.dataTransfer.types.includes('text/plain') && !isTextArea(event.target)) {
const keyword = event.dataTransfer.getData('text/plain');
const url = 'https://www.google.com/search?q=%s'.replace(/%s/gi, encodeURIComponent(keyword));
window.open(url, '_blank');
event.preventDefault();
}
};
document.addEventListener('dragover', handleDragOver, false);
document.addEventListener('drop', handleDrop, false);
})();