Select text inside a link like Opera

Disable link draging and select text.

当前为 2014-08-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Select text inside a link like Opera
  3. // @namespace eight04.blogspot.com
  4. // @description Disable link draging and select text.
  5. // @include http://*
  6. // @include https://*
  7. // @version 3.0
  8. // @grant GM_addStyle
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. /**
  13.  
  14. With this script, you can force the firefox to select text inside links,
  15. instead of dragging them around. If you need to drag them, just hold the ctrl
  16. key when dragging.
  17.  
  18. ![](https://i.imgur.com/f7TgRur.png)
  19. ![](https://i.imgur.com/NSqXG5n.png)
  20.  
  21. * Version 3.0 (Aug 17, 2014)
  22. - Rewrite with my coding style.
  23.  
  24. */
  25.  
  26. var force = {
  27. handleEvent: function(e){
  28. if(e.type == "click"){
  29. if(!this.initialized){
  30. return;
  31. }
  32. if(getSelection().toString()){
  33. e.preventDefault();
  34. e.stopPropagation();
  35. }
  36. this.uninit();
  37. }else if(e.type == "mousedown"){
  38. if(e.button || e.ctrlKey || e.altKey || e.shiftKey){
  39. return;
  40. }
  41. if(e.target.nodeName == "IMG"){
  42. return;
  43. }
  44. var a = e.target;
  45. while(a.nodeName != "A" && a.nodeName != "HTML"){
  46. a = a.parentNode;
  47. }
  48. if(!a.href){
  49. return;
  50. }
  51. this.init(a);
  52. }
  53. },
  54. init: function(a){
  55. this.initialized = true;
  56. this.cached = a.getAttribute("draggable");
  57. this.link = a;
  58. a.classList.add("force-select");
  59. a.draggable = false;
  60. },
  61. uninit: function(){
  62. this.initialized = false;
  63. if(this.cached === null){
  64. this.link.removeAttribute("draggable");
  65. }else{
  66. this.link.draggable = this.cached;
  67. }
  68. this.link.classList.remove("force-select");
  69. }
  70. };
  71.  
  72. document.addEventListener("mousedown", force, false);
  73. document.addEventListener("click", force, true);
  74. document.addEventListener("DOMContentLoaded", function(){
  75. GM_addStyle(".force-select{ -moz-user-select: text!important; }");
  76. }, false);