Email filler

Fills your email on input and text fields

目前为 2016-06-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Email filler
  3. // @namespace https://github.com/Farow/userscripts
  4. // @description Fills your email on input and text fields
  5. // @include *
  6. // @version 1.0.0
  7. // @grant GM_setValue
  8. // @grant GM_getValue
  9. // ==/UserScript==
  10.  
  11. const emails = [
  12. 'work.email@example.com',
  13. 'personal.email@example.com',
  14. ];
  15.  
  16. document.addEventListener('keypress', keypress);
  17.  
  18. function keypress (event) {
  19. if (event.code != 'KeyE') {
  20. return;
  21. }
  22.  
  23. if (!event.ctrlKey) {
  24. return;
  25. }
  26.  
  27. if (event.target.tagName != 'INPUT' && event.target.tagName != 'TEXTAREA') {
  28. return;
  29. }
  30.  
  31. fill_email(event.target);
  32. event.preventDefault();
  33. }
  34.  
  35. function fill_email (target) {
  36. const email = emails.shift();
  37.  
  38. /* save the selection as it gets reset when changing the input value */
  39. const selection_start = target.selectionStart;
  40.  
  41. target.value = (
  42. /* before selection */
  43. target.value.substring(0, selection_start) +
  44.  
  45. /* replace selection */
  46. email +
  47.  
  48. /* after selection */
  49. target.value.substring(target.selectionEnd)
  50. );
  51.  
  52. target.setSelectionRange(selection_start, selection_start + email.length);
  53. emails.push(email);
  54. }