Multi-click to select code

Multi-click a code snippet to select everything in the code block.

目前为 2020-04-28 提交的版本。查看 最新版本

  1. // This program is free software: you can redistribute it and/or modify
  2. // it under the terms of the GNU General Public License as published by
  3. // the Free Software Foundation, either version 3 of the License, or
  4. // (at your option) any later version.
  5. //
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. //
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. //
  14.  
  15. // ==UserScript==
  16. // @namespace https://gitlab.com/kekumu
  17. // @name Multi-click to select code
  18. // @description Multi-click a code snippet to select everything in the code block.
  19. // @version 1.0.0
  20. // ==OpenUserJS==
  21. // @author kekumu; https://gitlab.com/kekumu
  22. // ==/OpenUserJS==
  23. // @copyright 2020, kekumu (https://gitlab.com/kekumu)
  24. // @license GPL-3.0-or-later; https://www.gnu.org/licenses/gpl-3.0.html
  25. // @homepage https://github.com/kekumu/userscripts/lib/click2select.js
  26. // @supportURL https://github.com/kekumu/userscripts/labels/click2select.js
  27. // ==/UserLibrary==
  28. // @include *
  29. // @grant GM.getValue
  30. // @grant GM.setValue
  31. // ==/UserScript==
  32.  
  33. (async () => {
  34. "use strict";
  35.  
  36. const DEFAULT_SETTINGS = {4: 'code', 5: 'pre'};
  37.  
  38. let user_settings = await GM.getValue('settings');
  39.  
  40. /*
  41. * Some extensions (e.g. Tampermonkey) hide the storage/values menu if
  42. * there's no data saved there. This will make it appear so that you can
  43. * edit the settings.
  44. */
  45. if (!user_settings) {
  46. GM.setValue('settings', {});
  47. user_settings = {};
  48. }
  49.  
  50. const settings = Object.assign(DEFAULT_SETTINGS, user_settings);
  51.  
  52. function clickHandler(num, selector) {
  53. return event => {
  54. if (event.detail === num) {
  55. let el = event.target.closest(selector);
  56. if (el) {
  57. window.getSelection().selectAllChildren(event.target.closest(selector));
  58. event.preventDefault();
  59. }
  60. }
  61. }
  62. }
  63.  
  64. for (const prop in settings) {
  65. document.addEventListener('mousedown', clickHandler(Number(prop), settings[prop]));
  66. }
  67.  
  68. })();