Drag text to search

Highlight, drag, then release text to search in Google

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);
})();