Select text inside a link like Opera

Disable link draging and select text.

当前为 2014-05-11 提交的版本,查看 最新版本

  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 2.0.4
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. function OPLLS(){
  12. this.init.apply(this,arguments);
  13. }
  14.  
  15. OPLLS.prototype={
  16. handleEvent: function(e){
  17. switch(e.type){
  18. case "mouseup":
  19. if(!getSelection().toString())break;
  20. // console.log("mouseup");
  21. var t=e.target;
  22. while(t.nodeName!="A" && t.nodeName!="HTML")t=t.parentNode;
  23. if(!t.href){
  24. // console.log("uninit");
  25. this.uninit();
  26. }
  27. break;
  28. case "click":
  29. if(!getSelection().toString()){
  30. // console.log("clicked and uninit");
  31. this.uninit();
  32. break;
  33. }
  34. e.preventDefault();
  35. e.stopPropagation();
  36. // console.log("selected and uninit");
  37. this.uninit();
  38. }
  39. },
  40. init: function(e){
  41. var t=e.target;
  42. if(t.nodeName=="IMG")return;
  43. while(t.nodeName!="A" && t.nodeName!="HTML")t=t.parentNode;
  44. if(!t.href)return;
  45. this._draggable = t.getAttribute("draggable");
  46. // console.log(this._draggable);
  47. t.draggable=false;
  48. this.ele = t;
  49. // console.log("OK");
  50. document.addEventListener("mouseup",this,true);
  51. document.addEventListener("click",this,true);
  52. },
  53. uninit: function(){
  54. document.removeEventListener("mouseup",this,true);
  55. document.removeEventListener("click",this,true);
  56. if(this._draggable === null)
  57. this.ele.removeAttribute("draggable");
  58. else
  59. this.ele.setAttribute("draggable", this._draggable.toString());
  60. }
  61. }
  62.  
  63. document.addEventListener("mousedown",function(e){
  64. if(e.button!=0 || e.ctrlKey || e.altKey || e.shiftKey)return;
  65. new OPLLS(e);
  66. },false);