WME Virtual Keyboard

Adds a "virtual keyboard" to type locale-specific characters (currently only Turkish characters).

当前为 2017-11-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Virtual Keyboard
  3. // @namespace http://tampermonkey.net/
  4. // @version 2017.11.20.001
  5. // @description Adds a "virtual keyboard" to type locale-specific characters (currently only Turkish characters).
  6. // @author MapOMatic
  7. // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
  8. // @license GNU GPLv3
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function hidePopup($popup, $parentMenu) {
  16. var timeoutId = setTimeout(function() {
  17. $popup.css({display:'none'});
  18. $parentMenu.css({'border-radius':'8px 8px 8px 8px', 'background-color':'#999', 'box-shadow':''});
  19. }, 100);
  20. $popup.data('timeoutId', timeoutId);
  21. }
  22.  
  23. function showPopup($popup, $parentMenu) {
  24. $popup.css({display:'inherit'});
  25. $parentMenu.css({'border-radius':'8px 8px 0px 0px', 'background-color':'#777', 'box-shadow':'#b2b2b2 2px 2px 8px'});
  26. clearTimeout($popup.data('timeoutId'));
  27. }
  28.  
  29. function buttonOnMouseDown(evt) {
  30. evt.preventDefault();
  31. var char = $(evt.target).text();
  32. var element = document.activeElement;
  33. var $element = $(element);
  34. if (element && ($element.is('input:text') || $element.is('textarea'))) {
  35. var text = $element.val();
  36. var start = element.selectionStart;
  37. var end = element.selectionEnd;
  38. text = text.slice(0,start) + char + text.slice(end);
  39. $element.val(text);
  40. element.selectionStart = start+1;
  41. element.selectionEnd = start+1;
  42. $element.change();
  43. }
  44. }
  45.  
  46. function init() {
  47. console.log('WME More Letters: Initializing...');
  48.  
  49. var $menuParentDiv = $('#advanced-tools');
  50.  
  51. var $menuDiv = $('<div>', {id:'more-letters-menu'})
  52. .css({width:'56px', height:'18px', left:'10px', top:'27px', 'background-color': '#999', 'padding-top':'1px',
  53. 'z-index':'2000', 'margin-top':'-12px', 'margin-bottom':'2px', cursor:'default', 'font-size':'12px',
  54. 'text-align':'center', color:'white', 'border-radius':'8px 8px 8px 8px'})
  55. .text('Turkish')
  56. .appendTo($menuParentDiv);
  57.  
  58. var offset = $menuDiv.position();
  59.  
  60. var $popupDiv = $('<div>', {id:'more-letters-popup'})
  61. .css({padding:'2px', left:offset.left, top:offset.top + $menuDiv.height(), 'background-color': '#f8f8f8',
  62. 'box-shadow':'#b2b2b2 2px 2px 8px', 'z-index':'2001', 'margin-top':'-11px', cursor:'default', 'font-size':'13px',
  63. 'text-align':'center', color:'black', display:'none', position:'absolute'})
  64. .appendTo($menuParentDiv);
  65.  
  66. var chars = [['Ç','ç'],['Ğ','ğ'],['I','ı'],['İ','i'],['Ö','ö'],['Ş','ş'],['Ü','ü']];
  67. var $table = $('<table>');
  68. var buttonStyle = {width:'22px',height:'22px',padding:'unset'};
  69. var tdStyle = {padding:'4px 4px'};
  70. chars.forEach(function(pair) {
  71. $table.append($('<tr>').append(
  72. $('<td>').css(tdStyle).append($('<button>', {class:'waze-btn waze-btn-white'}).text(pair[0]).css(buttonStyle).mousedown(buttonOnMouseDown)),
  73. $('<td>').css(tdStyle).append($('<button>', {class:'waze-btn waze-btn-white'}).text(pair[1]).css(buttonStyle).mousedown(buttonOnMouseDown))
  74. ));
  75. });
  76. $popupDiv.append($table);
  77.  
  78. $menuDiv.mousedown(function(evt) {
  79. evt.preventDefault();
  80. showPopup($popupDiv, $menuDiv);
  81. clearTimeout($menuDiv.data('timeoutId'));
  82. }).mouseenter(function() {
  83. var timeoutId = setTimeout(function() {
  84. showPopup($popupDiv, $menuDiv);
  85. }, 100);
  86. $menuDiv.data('timeoutId', timeoutId);
  87. }).mouseleave(function() {
  88. hidePopup($popupDiv, $menuDiv);
  89. clearTimeout($menuDiv.data('timeoutId'));
  90. });
  91.  
  92. $popupDiv.mouseleave(function() {
  93. hidePopup($popupDiv, $menuDiv);
  94. }).mouseenter(function() {
  95. showPopup($popupDiv, $menuDiv);
  96. }).mousedown(function(evt) {
  97. evt.preventDefault();
  98. });
  99. }
  100.  
  101. function bootstrap(retries) {
  102. retries = retries || 0;
  103. if (retries === 100) {
  104. console.log('WME More Letters: Initialization failed. Exiting script.');
  105. return;
  106. }
  107. if (window.require && W && W.loginManager &&
  108. W.map && W.loginManager.isLoggedIn() && $('#advanced-tools').length) {
  109. init();
  110. } else {
  111. retries += 1;
  112. setTimeout(function () {
  113. bootstrap(retries);
  114. }, 250);
  115. }
  116. }
  117.  
  118. bootstrap();
  119. })();