tt-Google-Translate

highlight keywords and double tap 't' key

  1. // ==UserScript==
  2. // @name tt-Google-Translate
  3. // @namespace tt-Google-Translate
  4. // @description highlight keywords and double tap 't' key
  5. // @version 1
  6. // @grant none
  7. // @include *
  8. // ==/UserScript==
  9. var reset_function;
  10. var current_key_state = 0;
  11. var translate_string;
  12. var google_translate_final_url;
  13. var google_translate_api = "https://translate.google.com.hk/?hl=en&tab=wT#en/zh-TW/";
  14. /*
  15. var query_specials_map = {};
  16. query_specials_map["%"] = "%25";
  17. query_specials_map["&"] = "%26";
  18. */
  19. function translate_query_parser(text){
  20. /*
  21. for (var key in query_specials_map) {
  22. text = text.split(key).join(query_specials_map[key]);
  23. }
  24. */
  25. return text;
  26. }
  27. function translate(){
  28. translate_string = translate_query_parser(window.getSelection().toString());
  29. if (""===translate_string) return;
  30. google_translate_final_url = google_translate_api
  31. + translate_string;
  32. window.open(google_translate_final_url, "Powered By tt-Google-Translate",
  33. "toolbar=yes,menubar=yes,resizable=yes,titlebar=yes.location=yes,scrollbars=yes,status=yes,width=" +
  34. screen.width + ",height=" + screen.height);
  35. }
  36. document.addEventListener('keydown', function(event) {
  37. //alert(event.keyCode.toString());
  38. if (event.keyCode == 84) {
  39. if(current_key_state == 0){
  40. current_key_state = 1;
  41. //clearTimeout(reset_function);
  42. reset_function = setTimeout(function(){
  43. current_key_state = 0;
  44. }, 300);
  45. return;
  46. }
  47. if(current_key_state == 1){
  48. current_key_state = 0;
  49. clearTimeout(reset_function);
  50. translate();
  51. return;
  52. }
  53. }
  54. current_key_state = 0;
  55. clearTimeout(reset_function);
  56. }, true);