Double Click

Double Click to Lookup in Online Dictionary

  1. // ==UserScript==
  2. // @name Double Click
  3. // @namespace derjoker
  4. // @version 0.1.3
  5. // @description Double Click to Lookup in Online Dictionary
  6. // @author Feng Ya
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. ;(function () {
  12. 'use strict'
  13.  
  14. // Your code here...
  15. console.log('Double Click v' + GM_info.script.version)
  16.  
  17. const engines = {
  18. en: 'https://www.merriam-webster.com/dictionary/',
  19. de: 'https://www.duden.de/suchen/dudenonline/'
  20. }
  21.  
  22. const htmlLang = document.querySelector('html').lang.slice(0, 2)
  23.  
  24. window.addEventListener('dblclick', event => {
  25. console.log(event.path)
  26. console.log(event.path.map(node => node.lang))
  27. const langs = event.path.map(node => node.lang)
  28. .filter(lang => Boolean(lang))
  29. .map(lang => lang.slice(0, 2))
  30. const lang = htmlLang === 'en' ? langs[0] : htmlLang
  31. const engine = engines[lang] || engines.en
  32.  
  33. const selection =
  34. window.getSelection() ||
  35. document.getSelection() ||
  36. document.selection.createRange()
  37. const link = engine + selection
  38. console.log(link)
  39. window.open(link)
  40. })
  41. })()