Draggy

Drag a link to open in a new tab; drag a piece of text to search in a new tab.

目前為 2025-01-20 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Draggy
  3. // @name:zh-CN Draggy
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.2.6
  6. // @description Drag a link to open in a new tab; drag a piece of text to search in a new tab.
  7. // @description:zh-CN 拖拽链接以在新标签页中打开,拖拽文本以在新标签页中搜索。
  8. // @tag productivity
  9. // @author PRO-2684
  10. // @match *://*/*
  11. // @run-at document-start
  12. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  13. // @license gpl-3.0
  14. // @grant GM_addElement
  15. // @grant GM_openInTab
  16. // @grant GM_setValue
  17. // @grant GM_getValue
  18. // @grant GM_deleteValue
  19. // @grant GM_registerMenuCommand
  20. // @grant GM_unregisterMenuCommand
  21. // @grant GM_addValueChangeListener
  22. // @require https://github.com/PRO-2684/GM_config/releases/download/v1.2.1/config.min.js#md5=525526b8f0b6b8606cedf08c651163c2
  23. // ==/UserScript==
  24.  
  25. (function () {
  26. "use strict";
  27. const { name, version } = GM.info.script;
  28. const configDesc = {
  29. $default: {
  30. autoClose: false,
  31. },
  32. appearance: {
  33. name: "🎨 Appearance settings",
  34. title: "Settings for the appearance of Draggy overlay.",
  35. type: "folder",
  36. items: {
  37. circleOverlay: {
  38. name: "Circle overlay",
  39. title: "When to show the circle overlay.",
  40. value: 1,
  41. input: (prop, orig) => (orig + 1) % 3,
  42. processor: "same",
  43. formatter: (prop, value, desc) => desc.name + ": " + ["Never", "Auto", "Always"][value],
  44. },
  45. },
  46. },
  47. operation: {
  48. name: "🛠️ Operation settings",
  49. title: "Settings for the operation of Draggy.",
  50. type: "folder",
  51. items: {
  52. openTabInBg: {
  53. name: "Open tab in background",
  54. title: "Whether to open new tabs in the background.",
  55. type: "bool",
  56. value: false,
  57. },
  58. openTabInsert: {
  59. name: "Open tab insert",
  60. title: "Whether to insert the new tab next to the current tab. If false, the new tab will be appended to the end.",
  61. type: "bool",
  62. value: true,
  63. },
  64. matchingUriInText: {
  65. name: "Matching URI in text",
  66. title: "Whether to match URI in the selected text. If enabled AND the selected text is a valid URI AND its protocol is allowed, Draggy will open it directly instead of searching.",
  67. type: "bool",
  68. value: true,
  69. },
  70. minDistance: {
  71. name: "Minimum drag distance",
  72. title: "Minimum distance to trigger draggy.",
  73. type: "int", // 1-1000
  74. min: 1,
  75. max: 1000,
  76. value: 50,
  77. },
  78. },
  79. },
  80. searchEngine: {
  81. name: "🔎 Search engine settings",
  82. title: "Configure search engines for different directions. Use `{<max-length>}` as a placeholder for the URL-encoded query, where `<max-length>` is the maximum text length. If `<max-length>` is not specified, the search term will not be truncated.",
  83. type: "folder",
  84. items: {
  85. default: {
  86. name: "Search engine (default)",
  87. title: "Default search engine used when dragging text.",
  88. type: "string",
  89. value: "https://www.google.com/search?q={50}",
  90. },
  91. left: {
  92. name: "Search engine (left)",
  93. title: "Search engine used when dragging text left. Leave it blank to use the default search engine.",
  94. type: "string",
  95. value: ""
  96. },
  97. right: {
  98. name: "Search engine (right)",
  99. title: "Search engine used when dragging text right. Leave it blank to use the default search engine.",
  100. type: "string",
  101. value: ""
  102. },
  103. up: {
  104. name: "Search engine (up)",
  105. title: "Search engine used when dragging text up. Leave it blank to use the default search engine.",
  106. type: "string",
  107. value: ""
  108. },
  109. down: {
  110. name: "Search engine (down)",
  111. title: "Search engine used when dragging text down. Leave it blank to use the default search engine.",
  112. type: "string",
  113. value: ""
  114. },
  115. },
  116. },
  117. advanced: {
  118. name: "⚙️ Advanced settings",
  119. title: "Settings for advanced users or debugging.",
  120. type: "folder",
  121. items: {
  122. allowedProtocols: {
  123. name: "Allowed protocols",
  124. title: "Comma-separated list of allowed protocols for matched URI in texts. Leave it blank to allow all protocols.",
  125. type: "string",
  126. value: "http,https,ftp,mailto,tel",
  127. },
  128. maxTimeDelta: {
  129. name: "Maximum time delta",
  130. title: "Maximum time difference between esc/drop and dragend events to consider them as separate user gesture. Usually there's no need to change this value.",
  131. type: "int", // 1-100
  132. min: 1,
  133. max: 100,
  134. value: 10,
  135. },
  136. debug: {
  137. name: "Debug mode",
  138. title: "Enables debug mode.",
  139. type: "bool",
  140. value: false,
  141. },
  142. },
  143. },
  144. };
  145. const config = new GM_config(configDesc, { immediate: true });
  146. /**
  147. * Last time a drop event occurred.
  148. * @type {number}
  149. */
  150. let lastDrop = 0;
  151. /**
  152. * Start position of the drag event.
  153. * @type {{ x: number, y: number }}
  154. */
  155. let startPos = { x: 0, y: 0 };
  156. /**
  157. * Circle overlay.
  158. * @type {HTMLDivElement}
  159. */
  160. const circle = initOverlay();
  161. /**
  162. * Judging criteria for draggy.
  163. * @type {{ selection: (e: DragEvent) => string|HTMLAnchorElement|HTMLImageElement|null, handlers: (e: DragEvent) => boolean, dropEvent: (e: DragEvent) => boolean, }}
  164. */
  165. const judging = {
  166. selection: (e) => {
  167. const target = e.composedPath()[0];
  168. const img = target?.closest?.("img[src]");
  169. const src = img?.src;
  170. if (src) {
  171. return img;
  172. }
  173. const link = target?.closest?.("a[href]");
  174. const href = link?.getAttribute("href");
  175. if (href && !href.startsWith("javascript:") && href !== "#") {
  176. return link;
  177. }
  178. const selection = window.getSelection();
  179. const selectionAncestor = commonAncestor(selection.anchorNode, selection.focusNode);
  180. const selectedText = selection.toString();
  181. // Check if we're dragging the selected text (selectionAncestor is the ancestor of target, or target is the ancestor of selectionAncestor)
  182. if (selectedText && selectionAncestor && (isAncestorOf(selectionAncestor, target) || isAncestorOf(target, selectionAncestor))) {
  183. return selectedText;
  184. }
  185. },
  186. handlers: (e) => e.dataTransfer.dropEffect === "none" && e.dataTransfer.effectAllowed === "uninitialized" && !e.defaultPrevented,
  187. dropEvent: (e) => e.timeStamp - lastDrop > config.get("advanced.maxTimeDelta"),
  188. };
  189.  
  190. /**
  191. * Logs the given arguments if debug mode is enabled.
  192. * @param {...any} args The arguments to log.
  193. */
  194. function log(...args) {
  195. if (config.get("advanced.debug")) {
  196. console.log(`[${name}]`, ...args);
  197. }
  198. }
  199. /**
  200. * Finds the most recent common ancestor of two nodes.
  201. * @param {Node} node1 The first node.
  202. * @param {Node} node2 The second node.
  203. * @returns {Node|null} The common ancestor of the two nodes.
  204. */
  205. function commonAncestor(node1, node2) {
  206. const ancestors = new Set();
  207. for (let n = node1; n; n = n.parentNode) {
  208. ancestors.add(n);
  209. }
  210. for (let n = node2; n; n = n.parentNode) {
  211. if (ancestors.has(n)) {
  212. return n;
  213. }
  214. }
  215. return null;
  216. }
  217. /**
  218. * Checks if the given node is an ancestor of another node.
  219. * @param {Node} ancestor The ancestor node.
  220. * @param {Node} descendant The descendant node.
  221. * @returns {boolean} Whether the ancestor is an ancestor of the descendant.
  222. */
  223. function isAncestorOf(ancestor, descendant) {
  224. for (let n = descendant; n; n = n.parentNode) {
  225. if (n === ancestor) {
  226. return true;
  227. }
  228. }
  229. return false
  230. }
  231. /**
  232. * Opens the given URL in a new tab, respecting the user's preference.
  233. * @param {string} url The URL to open.
  234. */
  235. function open(url) {
  236. GM_openInTab(url, { active: !config.get("operation.openTabInBg"), insert: config.get("operation.openTabInsert") });
  237. }
  238. /**
  239. * Handles the given text based on the drag direction. If the text is a valid URI and protocol is allowed, open the URI; otherwise, search for the text.
  240. * @param {string} text The text to handle.
  241. * @param {string} direction The direction of the drag.
  242. */
  243. function handleText(text, direction) {
  244. if (URL.canParse(text)) {
  245. const url = new URL(text);
  246. const allowedProtocols = config.get("advanced.allowedProtocols").split(",").map(p => p.trim()).filter(Boolean);
  247. if (allowedProtocols.length === 0 || allowedProtocols.includes(url.protocol.slice(0, -1))) {
  248. open(text);
  249. return;
  250. }
  251. }
  252. search(text, direction);
  253. }
  254. /**
  255. * Searches for the given keyword.
  256. * @param {string} keyword The keyword to search for.
  257. * @param {string} direction The direction of the drag.
  258. */
  259. function search(keyword, direction) {
  260. const searchEngine = config.get(`searchEngine.${direction}`) || config.get("searchEngine.default");
  261. const maxLenMatch = searchEngine.match(/\{(\d*)\}/);
  262. const maxLenParsed = parseInt(maxLenMatch?.[1]);
  263. const maxLen = isNaN(maxLenParsed) ? +Infinity : maxLenParsed;
  264. const truncated = keyword.slice(0, maxLen);
  265. const url = searchEngine.replace(maxLenMatch[0], encodeURIComponent(truncated));
  266. log(`Searching for "${truncated}" using "${url}"`);
  267. open(url);
  268. }
  269. /**
  270. * Updates the circle overlay size.
  271. * @param {number} size The size of the circle overlay.
  272. */
  273. function onMinDistanceChange(size) {
  274. circle.style.setProperty("--size", size + "px");
  275. }
  276. /**
  277. * Creates a circle overlay.
  278. * @returns {HTMLDivElement} The circle overlay.
  279. */
  280. function initOverlay() {
  281. const circle = document.body.appendChild(document.createElement("div"));
  282. circle.id = "draggy-overlay";
  283. const textContent = `
  284. body > #draggy-overlay {
  285. --size: 50px; /* Circle radius */
  286. --center-x: calc(-1 * var(--size)); /* Hide the circle by default */
  287. --center-y: calc(-1 * var(--size));
  288. display: none;
  289. position: fixed;
  290. box-sizing: border-box;
  291. width: calc(var(--size) * 2);
  292. height: calc(var(--size) * 2);
  293. top: calc(var(--center-y) - var(--size));
  294. left: calc(var(--center-x) - var(--size));
  295. border-radius: 50%;
  296. border: 1px solid white; /* Circle border */
  297. padding: 0;
  298. margin: 0;
  299. mix-blend-mode: difference; /* Invert the background */
  300. background: transparent;
  301. z-index: 2147483647;
  302. pointer-events: none;
  303. &[data-draggy-overlay="0"] { }
  304. &[data-draggy-overlay="1"][data-draggy-selected] { display: block; }
  305. &[data-draggy-overlay="2"] { display: block; }
  306. }
  307. `;
  308. function addStyle() {
  309. if (document.getElementById("draggy-style")) {
  310. return;
  311. }
  312. GM_addElement(document.documentElement, "style", {
  313. id: "draggy-style",
  314. class: "darkreader", // Make Dark Reader ignore
  315. textContent
  316. });
  317. }
  318. addStyle();
  319. setTimeout(addStyle, 1000); // Dark Reader might remove the style
  320. return circle;
  321. }
  322. /**
  323. * Toggles the circle overlay.
  324. * @param {number} mode When to show the circle overlay.
  325. */
  326. function toggleOverlay(mode) {
  327. circle.setAttribute("data-draggy-overlay", mode);
  328. }
  329.  
  330. // Event listeners
  331. document.addEventListener("drop", (e) => {
  332. lastDrop = e.timeStamp;
  333. log("Drop event at", e.timeStamp);
  334. }, { passive: true });
  335. document.addEventListener("dragstart", (e) => {
  336. if (!judging.selection(e)) {
  337. circle.toggleAttribute("data-draggy-selected", false);
  338. } else {
  339. circle.toggleAttribute("data-draggy-selected", true);
  340. }
  341. const { x, y } = e;
  342. startPos = { x, y };
  343. circle.style.setProperty("--center-x", x + "px");
  344. circle.style.setProperty("--center-y", y + "px");
  345. log("Drag start at", startPos);
  346. }, { passive: true });
  347. document.addEventListener("dragend", (e) => {
  348. circle.style.removeProperty("--center-x");
  349. circle.style.removeProperty("--center-y");
  350. if (!judging.handlers(e)) {
  351. log("Draggy interrupted by other handler(s)");
  352. return;
  353. }
  354. if (!judging.dropEvent(e)) {
  355. log("Draggy interrupted by drop event");
  356. return;
  357. }
  358. const { x, y } = e;
  359. const [dx, dy] = [x - startPos.x, y - startPos.y];
  360. const distance = Math.hypot(dx, dy);
  361. if (distance < config.get("operation.minDistance")) {
  362. log("Draggy interrupted by short drag distance:", distance);
  363. return;
  364. }
  365. log("Draggy starts processing...");
  366. e.preventDefault();
  367. const data = judging.selection(e);
  368. if (data instanceof HTMLAnchorElement) {
  369. open(data.href);
  370. } else if (data instanceof HTMLImageElement) {
  371. open(data.src);
  372. } else if (typeof data === "string") {
  373. // Judge direction of the drag (Up, Down, Left, Right)
  374. const isVertical = Math.abs(dy) > Math.abs(dx);
  375. const isPositive = isVertical ? dy > 0 : dx > 0;
  376. const direction = isVertical ? (isPositive ? "down" : "up") : (isPositive ? "right" : "left");
  377. log("Draggy direction:", direction);
  378. handleText(data, direction);
  379. } else {
  380. log("Draggy can't find selected text or a valid link");
  381. }
  382. }, { passive: false });
  383.  
  384. // Dynamic configuration
  385. const callbacks = {
  386. "appearance.circleOverlay": toggleOverlay,
  387. "operation.minDistance": onMinDistanceChange,
  388. };
  389. for (const [prop, callback] of Object.entries(callbacks)) { // Initialize
  390. callback(config.get(prop));
  391. }
  392. config.addEventListener("set", (e) => { // Update
  393. const { prop, after } = e.detail;
  394. const callback = callbacks[prop];
  395. callback?.(after);
  396. });
  397.  
  398. log(`${version} initialized successfully 🎉`);
  399. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址