Greasy Fork

Select text inside a link like Opera

Disable link draging and select text.

目前为 2014-05-11 提交的版本。查看 最新版本

// ==UserScript==
// @name        Select text inside a link like Opera
// @namespace   eight04.blogspot.com
// @description Disable link draging and select text.
// @include     http://*
// @include     https://*
// @version     2.0.4
// @grant		none
// ==/UserScript==

function OPLLS(){
	this.init.apply(this,arguments);
}

OPLLS.prototype={
	handleEvent: function(e){
		switch(e.type){
			case "mouseup":
				if(!getSelection().toString())break;
				// console.log("mouseup");
				var t=e.target;
				while(t.nodeName!="A" && t.nodeName!="HTML")t=t.parentNode;
				if(!t.href){
					// console.log("uninit");
					this.uninit();
				}
				break;
			case "click":
				if(!getSelection().toString()){
					// console.log("clicked and uninit");
					this.uninit();
					break;
				}
				e.preventDefault();
				e.stopPropagation();
				// console.log("selected and uninit");
				this.uninit();
		}
	},
	init: function(e){
		var t=e.target;
		if(t.nodeName=="IMG")return;
		while(t.nodeName!="A" && t.nodeName!="HTML")t=t.parentNode;
		if(!t.href)return;
		this._draggable = t.getAttribute("draggable");
		// console.log(this._draggable);
		t.draggable=false;
		this.ele = t;
		// console.log("OK");
		document.addEventListener("mouseup",this,true);
		document.addEventListener("click",this,true);
	},
	uninit: function(){
		document.removeEventListener("mouseup",this,true);
		document.removeEventListener("click",this,true);
		if(this._draggable === null)
			this.ele.removeAttribute("draggable");
		else
			this.ele.setAttribute("draggable", this._draggable.toString());
	}
}

document.addEventListener("mousedown",function(e){
	if(e.button!=0 || e.ctrlKey || e.altKey || e.shiftKey)return;
	new OPLLS(e);
},false);