toMorse

Plays selected text as morse code.

目前为 2015-07-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name toMorse
  3. // @description Plays selected text as morse code.
  4. // @namespace https://greasyfork.org/en/users/11891-qon
  5. // @author Qon
  6. // @include *
  7. // @include about:toMorse
  8. // @noframes
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @license Simple Public License 2.0 (SimPL) https://tldrlegal.com/license/simple-public-license-2.0-%28simpl%29
  12. // @version 0.0.1.20150722035922
  13. // ==/UserScript==
  14.  
  15. /*
  16. TODO
  17. GUI
  18. Volume control
  19. Speed control
  20. Pitch control
  21. Set your own hotkey
  22. BUG
  23. perfect Selection animation
  24. */
  25.  
  26. var sett = {}
  27. sett['cwpm'] = {def: 20, val: 20, min: 1, max: 100, label: 'Letter WPM', description: 'cwpm'}
  28. sett['ewpm'] = {def: 20, val: 20, min: 1, max: 100, label: 'Effective WPM', description: 'ewpm'}
  29. sett['tone'] = {def: 800, val: 800, min: 200, max: 3200, label: 'Tone frequency', description: 'tone'}
  30. var current = {}
  31.  
  32. for (var key in sett) {
  33. if(sett.hasOwnProperty(key)) {
  34. sett[key]['val'] = GM_getValue(key, sett[key]['val'])
  35. }
  36. }
  37.  
  38. function playTextLCWO(charWpm, effectiveWpm, tone, str) {
  39. var prevAudio = document.getElementById('audioLCWO')
  40. if(prevAudio) {
  41. prevAudio.pause()
  42. prevAudio.remove()
  43. }
  44. var audio = document.createElement('audio')
  45. audio.id = 'audioLCWO'
  46. audio.src = 'http://cgi2.lcwo.net/cgi-bin/cw.ogg?s='+charWpm+'&e='+effectiveWpm+'&f='+tone+'&t='+str
  47. audio.addEventListener('ended', function(){document.getElementById('audioLCWO').remove()});
  48. document.body.appendChild(audio)
  49. audio.play()
  50. }
  51.  
  52. function keypress (e) {
  53. if (!e) e = window.event;
  54. if (e.ctrlKey && e.key == 'm') {
  55. playTextLCWO(sett['cwpm']['val'], sett['ewpm']['val'], sett['tone']['val'], window.getSelection().toString())
  56. }
  57. }
  58. window.addEventListener("keydown", keypress);
  59.  
  60. if (document.location == 'https://greasyfork.org/en/scripts/11117-tomorse') {
  61. var settingsdiv = document.createElement('div');
  62. settingsdiv.id = 'tomorse-settings'
  63. var h3 = document.createElement('h3')
  64. h3.innerHTML = 'Script Settings'
  65. h3.style.marginBottom = '0'
  66. h3.style.paddingBottom = '0'
  67. settingsdiv.appendChild(h3)
  68. var qontdiv = document.createElement('div');
  69. qontdiv.style.padding = '1em'
  70. qontdiv.style.margin = '0'
  71. qontdiv.style.background = '#E6FFE6'
  72.  
  73. function setting(name, variable, decription) {
  74. var span = document.createElement('span')
  75. span.innerHTML = name+': '
  76. var inpN = document.createElement('input')
  77. inpN.id = 'input-'+variable
  78. inpN.name = variable
  79. inpN.type = 'number'
  80. inpN.value = sett[variable]['val']
  81. var inpRange = document.createElement('input')
  82. inpRange.id = 'input-range-'+variable
  83. inpRange.name = variable
  84. inpRange.type = 'range'
  85. inpRange.min = sett[variable]['min']
  86. inpRange.max = sett[variable]['max']
  87. inpRange.value = sett[variable]['val']
  88. f = function(ev){
  89. var el = ev.target
  90. var inte = parseInt(el.value)
  91. GM_setValue(el.name, inte)
  92. sett[el.name]['val'] = inte
  93. document.getElementById('input-range-'+el.name).value = inte
  94. document.getElementById('input-'+el.name).value = inte
  95. if(el.name == 'cwpm' && document.getElementById('wpm-link').checked) {
  96. document.getElementById('input-range-'+'ewpm').value = inte
  97. document.getElementById('input-'+'ewpm').value = inte
  98. GM_setValue('ewpm', inte)
  99. sett['ewpm']['val'] = inte
  100. }
  101. }
  102. inpN.addEventListener('input', f)
  103. inpRange.addEventListener('input', f)
  104. qontdiv.appendChild(span)
  105. qontdiv.appendChild(inpN)
  106. qontdiv.appendChild(inpRange)
  107. qontdiv.appendChild(document.createElement('br'))
  108. }
  109.  
  110. for(var key in sett) {
  111. if(sett.hasOwnProperty(key)) {
  112. setting(sett[key]['label'], key, sett[key]['description'])
  113. }
  114. }
  115. var box = document.createElement('input')
  116. box.id = 'wpm-link'
  117. box.checked = true
  118. box.type = 'checkbox'
  119. var linkSpan = document.createElement('span')
  120. linkSpan.innerHTML = 'Copy value to "Effective WPM"'
  121. linkSpan.style.fontSize = '75%'
  122. linkSpan.style.marginLeft = '100px'
  123. // linkSpan.style.marginLeft = 'auto'
  124. qontdiv.insertBefore(linkSpan, qontdiv.getElementsByTagName('br')[0])
  125. qontdiv.insertBefore(box, qontdiv.getElementsByTagName('br')[0])
  126.  
  127. // console.log('QQQ')
  128. document.getElementById('script-content').appendChild(settingsdiv)
  129. settingsdiv.appendChild(qontdiv)
  130. }