Dictionary audio link revealer

Add audio file link of dictionary pages

  1. // ==UserScript==
  2. // @name Dictionary audio link revealer
  3. // @name:zh-TW 網路字典發音下載
  4. // @namespace https://github.com/solomonhuang/dictionary-audio-link-revealer
  5. // @version 0.20220112.0
  6. // @description Add audio file link of dictionary pages
  7. // @description:zh-TW 在網路字典的發音按鈕旁邊新增聲音檔下載。
  8. // @author Solomon Huang
  9. // @license MIT
  10. // @match https://dictionary.cambridge.org/*
  11. // @match https://tw.dictionary.search.yahoo.com/*
  12. // @match https://www.oxfordlearnersdictionaries.com/definition/*
  13. // @match https://www.dictionary.com/browse/*
  14. // @grant none
  15. // @supportURL https://github.com/solomonhuang/dictionary-audio-link-revealer/issues
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20.  
  21. var currentDict = location.hostname
  22.  
  23. function createICON(fontSZ) {
  24. let icon = document.createElementNS('http://www.w3.org/2000/svg','svg')
  25. // SVG icon alighment style
  26. // Elliot Dahl ( https://twitter.com/Elliotdahl )
  27. // https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4
  28. icon.setAttribute('style','height:1em;width:1em;top: .125em;position: relative;display: inline-flex;align-self: center;')
  29. icon.setAttribute('width', '24')
  30. icon.setAttribute('height', '24')
  31. icon.setAttribute('stroke-width', '1.5')
  32. icon.setAttribute('stroke-width', '1.5')
  33. icon.setAttribute('viewBox', '0 0 24 24')
  34. icon.setAttribute('fill', 'none')
  35.  
  36. let p3 = document.createElementNS('http://www.w3.org/2000/svg','path')
  37. p3.setAttribute('d', 'M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z')
  38. p3.setAttribute('stroke', 'green')
  39. p3.setAttribute('stroke-linecap', 'round')
  40. p3.setAttribute('stroke-linejoin', 'round')
  41. p3.setAttribute('fill', 'yellow')
  42. icon.appendChild(p3)
  43. let p1 = document.createElementNS('http://www.w3.org/2000/svg','path')
  44. p1.setAttribute('d', 'M9 17L15 17')
  45. p1.setAttribute('stroke', 'green')
  46. p1.setAttribute('stroke-linecap', 'round')
  47. p1.setAttribute('stroke-linejoin', 'round')
  48. icon.appendChild(p1)
  49. let p2 = document.createElementNS('http://www.w3.org/2000/svg','path')
  50. p2.setAttribute('d', 'M12 6V13M12 13L15.5 9.5M12 13L8.5 9.5')
  51. p2.setAttribute('stroke', 'green')
  52. p2.setAttribute('stroke-linecap', 'round')
  53. p2.setAttribute('stroke-linejoin', 'round')
  54. icon.appendChild(p2)
  55.  
  56. return icon
  57. }
  58.  
  59. function createLink(src) {
  60. let link = document.createElement('a')
  61. link.href = src
  62. link.style = 'text-decoration:none'
  63. return link
  64. }
  65.  
  66. function appendICON(el, src) {
  67. let fontSZ = parseFloat(getComputedStyle(el).fontSize)
  68. let link = createLink(src)
  69. link.appendChild(createICON(fontSZ))
  70. el.append(link)
  71. }
  72.  
  73. setTimeout(() => {
  74. /* all dictionary sites begin here */
  75.  
  76. if (/dictionary\.cambridge\.org/.test(currentDict)) {
  77. let daud = document.querySelectorAll('.daud')
  78. for (var i = 0; i < daud.length; i++) {
  79. let a = daud[i].getElementsByTagName('audio')
  80. let src = a[0].children[0].src
  81. appendICON(daud[i].children[1], src)
  82. }
  83. }
  84.  
  85. if (/tw\.dictionary\.search\.yahoo\.com/.test(currentDict)) {
  86. let dict_sound = document.querySelectorAll('.dict-sound')
  87. // TODO: fix with MutationObserver
  88. // https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
  89. for (let i = 0; i < dict_sound.length; i++) {
  90. let src = dict_sound[i].children[0].src
  91. appendICON(dict_sound[i].parentElement, src)
  92. }
  93. }
  94.  
  95. if (/www\.oxfordlearnersdictionaries\.com/.test(currentDict)) {
  96. let icon_audio = document.querySelectorAll('.icon-audio')
  97. for (let i = 0; i < icon_audio.length; i++) {
  98. let src = icon_audio[i].attributes["data-src-mp3"].value
  99. appendICON(icon_audio[i].parentElement, src)
  100. }
  101. }
  102.  
  103. if (/www\.dictionary\.com/.test(currentDict)) {
  104. let audio_wrapper = document.querySelectorAll('.audio-wrapper')
  105. for (let i = 0; i < audio_wrapper.length; i++) {
  106. let src = audio_wrapper[i].children[1].children[1].src
  107. appendICON(audio_wrapper[i], src)
  108. }
  109. }
  110.  
  111. /* all dictionary sites end here */
  112. }, 500)
  113. })();
  114.