DSG (Drag Search & Go) v1.7

Boosts productivity by letting you have selected text or links to instantly search or open

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         DSG (Drag Search & Go) v1.7
// @namespace    https://deeone.blog
// @version      1.7
// @description  Boosts productivity by letting you have selected text or links to instantly search or open
// @author       deoone
// @match        *://*/*
// @grant        GM_openInTab
// ==/UserScript==

(function() {
  'use strict';

  const SEARCH_BASE = 'https://www.google.com/search?q=';
  let isMouseDown    = false;
  let startX, startY;
  let moved          = false;
  let hadSelection   = false;
  let startTarget    = null;

  document.addEventListener('mousedown', e => {
    if (e.button !== 0) return;
    isMouseDown  = true;
    moved        = false;
    startX       = e.clientX;
    startY       = e.clientY;
    startTarget  = e.target;
    hadSelection = !window.getSelection().isCollapsed;
  });

  document.addEventListener('mousemove', e => {
    if (!isMouseDown || moved) return;
    const dx = e.clientX - startX;
    const dy = e.clientY - startY;
    if (Math.hypot(dx, dy) > 5) {
      moved = true;  // considered a drag if movement exceeds 5px?
    }
  });

  document.addEventListener('mouseup', e => {
    if (!isMouseDown) return;
    isMouseDown = false;
    if (!moved) return;  // was trying to make it so it only works if i drag but seems like that doesnt work

    let url = null;
    const sel = window.getSelection().toString().trim();

    if ((hadSelection || sel) && sel) {
      url = SEARCH_BASE + encodeURIComponent(sel);
    } else {
      // Otherwise, check if they dragged a link
      const link = e.target.closest('a') || (startTarget.closest && startTarget.closest('a'));
      if (link && link.href) {
        url = link.href;
      }
    }

    if (!url) return;

    // Handle how the URL is opened based on key modifiers
    if (e.ctrlKey && e.shiftKey) {
      // we use gm_openInTab to open a background tab, useful for saving tabs to look at later
      GM_openInTab(url, { active: false });
    } else if (e.ctrlKey) {
      // redirect to current tab
      window.location.href = url;
    } else {
      // open in new tab but move to that tab
      GM_openInTab(url, { active: true });
    }
  });
})();