AutoSaveDiv

save and restores div.contenteditable texts

当前为 2015-02-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        AutoSaveDiv
// @namespace   AutoSaveDiv
// @description save and restores div.contenteditable texts
// @include     *
// @exclude		https://docs.google.com/*
// @version     1.03
// @grant       GM_listValues
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_registerMenuCommand
// @grant       GM_deleteValue
// @author		Dediggefedde
// ==/UserScript==

var active=false;
var isCtrl=false;
var isShift=false;
var isAlt=false;
var name="";
var altstil;
var img=document.createElement("div");

GM_registerMenuCommand("Clear stored Formular Fiels", function(){
    var arrs=GM_listValues();
    var i=0;
   for (i = 0; i < cars.length; i++) { 
        GM_deleteValue(arrs[i]);
    }
}, "c" );

img.addEventListener("contextmenu",function(e){
	restore(img.previousSibling);
	e.preventDefault();
	return false;
},false);

img.addEventListener("click",function(e){
	e.preventDefault();
	if(e.which==1)listenView(img.previousSibling);
	// else if(e.which==3)
	return false;
},true);
		
function save(el){
	if(el.tagName=="DIV")
		GM_setValue(name,document.activeElement.innerHTML);
	else if(el.tagName=="TEXTAREA")
		GM_setValue(name,document.activeElement.value);
}
function restore(el){
	if(el.tagName=="DIV")
		el.innerHTML=GM_getValue(name);
	else if(el.tagName=="TEXTAREA")
		el.value=GM_getValue(name);
}
function listenView(el){
	
	active=!active;
	if(active){
		img.style.backgroundColor="red";
		altstil=el.style.border;
		el.style.border="1px solid red";
	}else {
		img.style.backgroundColor="blue";
		el.style.border=altstil;
	}
}

document.addEventListener("click",function(e){
	if(this==img||active)return false;
	if((document.activeElement.tagName=="DIV"&&document.activeElement.getAttribute("contenteditable"))||document.activeElement.tagName=="TEXTAREA"){
		name=location.host+"::"+document.activeElement.tagName+"_"+location.pathname;
		img.setAttribute("style","width:20px;height:20px;position:relative;margin-top:-20px;opacity:0.5;border:1px solid blue;background-color:blue;border-top-right-radius:15px;");
		img.title="leftclick: de-/activate capturing text!\nrightclick: restore last text!\nstored:\n"+GM_getValue(name);
		document.activeElement.parentNode.insertBefore(img, document.activeElement.nextSibling);
		// console.log(name);
	}	
},true);

document.addEventListener("keydown",function(e){
	switch(e.which){
		case 17:isCtrl=true;break;
		case 16:isShift=true;break;
		case 18:isAlt=true;break;
		default:
	};
},true);

document.addEventListener("keyup",function(e){
	switch(e.which){
		case 17:isCtrl=false;break;
		case 16:isShift=false;break;
		case 18:isAlt=false;break;
		case 68: //d
			if(isShift&&isCtrl&&isAlt){
				listenView(document.activeElement);
			}			
			break;
		case 83: //s
			if(!active&&isShift&&isCtrl&&isAlt)restore(document.activeElement);
			break;
		default:
	};
	if(active){
		save(document.activeElement);
		img.title="leftclick: de-/activate capturing text!\nrightclick: restore last text!\nstored:\n"+GM_getValue(name);
	}
},true);