Explorar el Código

minor fix

Signed-off-by: Gerald <[email protected]>
Gerald hace 11 años
padre
commit
ef8d9828b6
Se han modificado 5 ficheros con 258 adiciones y 250 borrados
  1. 0 2
      .gitignore
  2. 3 2
      src/background.js
  3. 248 240
      src/injected.js
  4. 7 6
      src/style.less
  5. 0 0
      src/style.min.css

+ 0 - 2
.gitignore

@@ -1,3 +1 @@
 dist/
-node_modules/
-*.bat

+ 3 - 2
src/background.js

@@ -1,5 +1,5 @@
 function getUniqId() {
-	return Date.now().toString(36)+Math.floor(Math.random()*0x100000).toString(36);
+	return Date.now().toString(36)+Math.random().toString(36).slice(2,6);
 }
 var db,port=null,pos=0;
 function notify(title,options) {
@@ -611,7 +611,8 @@ function exportZip(z,src,callback){
 }
 
 // Requests
-var requests={},request_id_map={},special_headers=['user-agent'];
+var requests={},request_id_map={},
+		special_headers=['user-agent','referer','origin'];
 function getRequestId(data,src,callback) {
   var id=getUniqId();
 	// XHR, finalUrl, requestId in browser

+ 248 - 240
src/injected.js

@@ -3,7 +3,7 @@
 if(window.VM) return;window.VM=1;
 
 function getUniqId(){
-	return Date.now().toString(36)+Math.floor(Math.random()*0x100000).toString(36);
+	return Date.now().toString(36)+Math.random().toString(36).slice(2,6);
 }
 /**
 * http://www.webtoolkit.info/javascript-utf8.html
@@ -82,11 +82,11 @@ var comm={
 		return p;
 	})(window,[]),
 	init:function(s,d){
-		var t=this;
-		t.sid=t.vmid+s;
-		t.did=t.vmid+d;
-		document.addEventListener(t.sid,t['handle'+s].bind(t),false);
-		t.load=t.checkLoad=function(){};
+		var comm=this;
+		comm.sid=comm.vmid+s;
+		comm.did=comm.vmid+d;
+		document.addEventListener(comm.sid,comm['handle'+s].bind(comm),false);
+		comm.load=comm.checkLoad=function(){};
 	},
 	post:function(d){
 		var e=document.createEvent("MutationEvent");
@@ -122,253 +122,260 @@ var comm={
 			console.log('Error running script: '+name+'\n'+e.message);
 		}
 	},
-	loadScript:function(o){
-		var start=[],idle=[],end=[],cache,require,values,comm=this,urls={},
-		Request=(function(){
-			// request functions
-			function reqAbort(){comm.post({cmd:'AbortRequest',data:this.id});}
+	initRequest:function(){
+		// request functions
+		function reqAbort(){comm.post({cmd:'AbortRequest',data:this.id});}
 
-			// request object functions
-			function callback(d){
-				var i,t=this,c=t.details['on'+d.type];
-				if(c) {
-					if(d.data.response) {
-						if(!t.data.length) {
-							if(d.resType) {	// blob or arraybuffer
-								var m=d.data.response.match(/^data:(.*?);base64,(.*)$/);
-								if(!m) d.data.response=null;
-								else {
-									var b=window.atob(m[2]);
-									if(t.details.responseType=='blob') {
-										t.data.push(new Blob([b],{type:m[1]}));
-									} else {	// arraybuffer
-										m=new Uint8Array(b.length);
-										for(i=0;i<b.length;i++) m[i]=b.charCodeAt(i);
-										t.data.push(m.buffer);
-									}
+		// request object functions
+		function callback(d){
+			var i,t=this,c=t.details['on'+d.type];
+			if(c) {
+				if(d.data.response) {
+					if(!t.data.length) {
+						if(d.resType) {	// blob or arraybuffer
+							var m=d.data.response.match(/^data:(.*?);base64,(.*)$/);
+							if(!m) d.data.response=null;
+							else {
+								var b=window.atob(m[2]);
+								if(t.details.responseType=='blob') {
+									t.data.push(new Blob([b],{type:m[1]}));
+								} else {	// arraybuffer
+									m=new Uint8Array(b.length);
+									for(i=0;i<b.length;i++) m[i]=b.charCodeAt(i);
+									t.data.push(m.buffer);
 								}
-							} else if(t.details.responseType=='json')	// json
-								t.data.push(JSON.parse(d.data.response));
-							else	// text
-								t.data.push(d.data.response);
-						}
-						d.data.response=t.data[0];
+							}
+						} else if(t.details.responseType=='json')	// json
+							t.data.push(JSON.parse(d.data.response));
+						else	// text
+							t.data.push(d.data.response);
 					}
-					c(d.data);
+					d.data.response=t.data[0];
 				}
-				if(d.type=='loadend') delete comm.requests[t.id];
+				c(d.data);
 			}
-			function start(id){
-				var t=this,data={
-					id:id,
-					method:t.details.method,
-					url:t.details.url,
-					data:t.details.data,
-					//async:!t.details.synchronous,
-					user:t.details.user,
-					password:t.details.password,
-					headers:t.details.headers,
-					overrideMimeType:t.details.overrideMimeType,
-				};
-				t.id=id;
-				comm.requests[id]=t;
-				if(comm.inArray(['arraybuffer','blob'],t.details.responseType)) data.responseType='blob';
-				comm.post({cmd:'HttpRequest',data:data});
-			}
-
-			return function(details){
-				var t={
-					details:details,
-					callback:callback,
-					start:start,
-					req:{
-						abort:reqAbort,
-					},
-					data:[],
-				},a=document.createElement('a');
-				a.setAttribute('href',details.url);
-				details.url=a.href;
-				comm.qrequests.push(t);
-				comm.post({cmd:'GetRequestId'});
-				return t.req;
+			if(d.type=='loadend') delete comm.requests[t.id];
+		}
+		function start(id){
+			var t=this,data={
+				id:id,
+				method:t.details.method,
+				url:t.details.url,
+				data:t.details.data,
+				//async:!t.details.synchronous,
+				user:t.details.user,
+				password:t.details.password,
+				headers:t.details.headers,
+				overrideMimeType:t.details.overrideMimeType,
 			};
-		})();
-		comm.requests={};comm.qrequests=[];
-		comm.command={};comm.ainject={};
+			t.id=id;
+			comm.requests[id]=t;
+			if(comm.inArray(['arraybuffer','blob'],t.details.responseType)) data.responseType='blob';
+			comm.post({cmd:'HttpRequest',data:data});
+		}
+		function getFullUrl(url){
+			var a=document.createElement('a');
+			a.setAttribute('href',url);
+			return a.href;
+		}
 
-		/*
-		 * Wrap functions and properties
-		 */
+		var comm=this;
+		comm.requests={};
+		comm.qrequests=[];
+		comm.Request=function(details){
+			var t={
+				details:details,
+				callback:callback,
+				start:start,
+				req:{
+					abort:reqAbort,
+				},
+				data:[],
+			};
+			details.url=getFullUrl(details.url);
+			comm.qrequests.push(t);
+			comm.post({cmd:'GetRequestId'});
+			return t.req;
+		};
+	},
+	initWrapper:function(){
 		// wrapper: wrap functions as needed, return and set properties
-		function wrapper(){
-			function wrapItem(key,wrap){
-				var type=null,value,apply=Function.apply;
-				function initProperty() {
-					if(!comm.inArray(['function','custom'],type)) {
-						value=window[key];
-						type=typeof value;
-						if(type=='function'&&wrap) {
-							var o=value;
-							value=function(){
-								var r;
-								try {
-									r=apply.apply(o,[window,arguments]);
-								} catch(e) {
-									console.log('[Violentmonkey] Error calling '+key+':\n'+e.stack);
-								}
-								return r===window?t:r;
-							};
-							value.__proto__=o;
-							value.prototype=o.prototype;
-						}
+		function wrapItem(key,thisObj,wrap){
+			var type=null,value,apply=Function.apply;
+			function initProperty() {
+				if(!comm.inArray(['function','custom'],type)) {
+					value=window[key];
+					type=typeof value;
+					if(wrap&&type=='function') {
+						var o=value;
+						value=function(){
+							var r;
+							try {
+								r=apply.apply(o,[window,arguments]);
+							} catch(e) {
+								console.log('[Violentmonkey] Error calling '+key+':\n'+e.stack);
+							}
+							return r===window?thisObj:r;
+						};
+						value.__proto__=o;
+						value.prototype=o.prototype;
 					}
 				}
-				try {
-					Object.defineProperty(t,key,{
-						get:function(){
-							initProperty();
-							return value===window?t:value;
-						},
-						set:function(v){
-							initProperty();
-							value=v;
-							if(type!='function') window[key]=v;
-							type='custom';
-						},
-					});
-				} catch(e) {
-					// ignore protected data
-				}
-			}
-			var t=this;
-			comm.forEach(comm.prop1,wrapItem,[]);
-			comm.forEach(comm.prop2,wrapItem,[true]);
-		}
-
-		function wrapGM(c){
-			// Add GM functions
-			// Reference: http://wiki.greasespot.net/Greasemonkey_Manual:API
-			var gm={},value=values[c.uri]||{},g=c.meta.grant||[];
-			if(!g.length||g.length==1&&g[0]=='none') {	// @grant none
-				g.pop();
-			} else {
-				gm['window']=new wrapper();
 			}
-			if(!comm.inArray(g,'unsafeWindow')) g.push('unsafeWindow');
-			function propertyToString(){return 'Property for Violentmonkey: designed by Gerald';}
-			function addProperty(name,prop,obj){
-				if('value' in prop) prop.writable=false;
-				prop.configurable=false;
-				Object.defineProperty(obj,name,prop);
-				if(typeof obj[name]=='function') obj[name].toString=propertyToString;
+			try {
+				Object.defineProperty(thisObj,key,{
+					get:function(){
+						initProperty();
+						return value===window?thisObj:value;
+					},
+					set:function(v){
+						initProperty();
+						value=v;
+						if(type!='function') window[key]=v;
+						type='custom';
+					},
+				});
+			} catch(e) {
+				// ignore protected data
 			}
-			var resources=c.meta.resources||{},gf={
-				unsafeWindow:{value:window},
-				GM_info:{get:function(){
-					var m=c.code.match(/\/\/\s+==UserScript==\s+([\s\S]*?)\/\/\s+==\/UserScript==\s/),
-							script={
-								description:c.meta.description||'',
-								excludes:c.meta.exclude.concat(),
-								includes:c.meta.include.concat(),
-								matches:c.meta.match.concat(),
-								name:c.meta.name||'',
-								namespace:c.meta.namespace||'',
-								resources:{},
-								'run-at':c.meta['run-at']||'document-end',
-								unwrap:false,
-								version:c.meta.version||'',
-							},
-							o={};
-					addProperty('script',{value:{}},o);
-					addProperty('scriptMetaStr',{value:m?m[1]:''},o);
-					addProperty('scriptWillUpdate',{value:c.update},o);
-					addProperty('version',{value:undefined},o);
-					for(m in script) addProperty(m,{value:script[m]},o.script);
-					for(m in c.meta.resources) addProperty(m,{value:c.meta.resources[m]},o.script.resources);
-					return o;
-				}},
-				GM_deleteValue:{value:function(key){delete value[key];comm.post({cmd:'SetValue',data:{uri:c.uri,values:value}});}},
-				GM_getValue:{value:function(k,d){
-					var v=value[k];
-					if(v) {
-						k=v[0];
-						v=v.slice(1);
-						switch(k){
-							case 'n': d=Number(v);break;
-							case 'b': d=v=='true';break;
-							case 'o': try{d=JSON.parse(v);}catch(e){console.log(e);}break;
-							default: d=v;
-						}
-					}
-					return d;
-				}},
-				GM_listValues:{value:function(){return Object.getOwnPropertyNames(value);}},
-				GM_setValue:{value:function(key,val){
-					var t=(typeof val)[0];
-					switch(t){
-						case 'o':val=t+JSON.stringify(val);break;
-						default:val=t+val;
-					}
-					value[key]=val;comm.post({cmd:'SetValue',data:{uri:c.uri,values:value}});
-				}},
-				GM_getResourceText:{value:function(name){
-					var i,u;
-					for(i in resources) if(name==i) {
-						u=cache[resources[i]];
-						if(u) u=comm.utf8decode(window.atob(u));
-						return u;
-					}
-				}},
-				GM_getResourceURL:{value:function(name){
-					var i,u,r,j,b;
-					for(i in resources) if(name==i) {
-						i=resources[i];u=urls[i];
-						if(!u&&(r=cache[i])) {
-							r=window.atob(r);
-							b=new Uint8Array(r.length);
-							for(j=0;j<r.length;j++) b[j]=r.charCodeAt(j);
-							b=new Blob([b]);
-							urls[i]=u=URL.createObjectURL(b);
-						}
-						return u;
+		}
+		var comm=this;
+		comm.Wrapper=function(){
+			comm.forEach(comm.prop1,wrapItem,[this]);
+			comm.forEach(comm.prop2,wrapItem,[this,true]);
+		};
+	},
+	wrapGM:function(c,value,cache){
+		// Add GM functions
+		// Reference: http://wiki.greasespot.net/Greasemonkey_Manual:API
+		var gm={},g=c.meta.grant||[],urls={},comm=this;
+		if(!g.length||g.length==1&&g[0]=='none') {	// @grant none
+			g.pop();
+		} else {
+			if(!comm.Wrapper) comm.initWrapper();
+			gm['window']=new comm.Wrapper();
+		}
+		value=value||{};
+		if(!comm.inArray(g,'unsafeWindow')) g.push('unsafeWindow');
+		function propertyToString(){return 'Property for Violentmonkey: designed by Gerald';}
+		function addProperty(name,prop,obj){
+			if('value' in prop) prop.writable=false;
+			prop.configurable=false;
+			Object.defineProperty(obj,name,prop);
+			if(typeof obj[name]=='function') obj[name].toString=propertyToString;
+		}
+		var resources=c.meta.resources||{},gf={
+			unsafeWindow:{value:window},
+			GM_info:{get:function(){
+				var m=c.code.match(/\/\/\s+==UserScript==\s+([\s\S]*?)\/\/\s+==\/UserScript==\s/),
+						script={
+							description:c.meta.description||'',
+							excludes:c.meta.exclude.concat(),
+							includes:c.meta.include.concat(),
+							matches:c.meta.match.concat(),
+							name:c.meta.name||'',
+							namespace:c.meta.namespace||'',
+							resources:{},
+							'run-at':c.meta['run-at']||'document-end',
+							unwrap:false,
+							version:c.meta.version||'',
+						},
+						o={};
+				addProperty('script',{value:{}},o);
+				addProperty('scriptMetaStr',{value:m?m[1]:''},o);
+				addProperty('scriptWillUpdate',{value:c.update},o);
+				addProperty('version',{value:undefined},o);
+				for(m in script) addProperty(m,{value:script[m]},o.script);
+				for(m in c.meta.resources) addProperty(m,{value:c.meta.resources[m]},o.script.resources);
+				return o;
+			}},
+			GM_deleteValue:{value:function(key){
+				delete value[key];comm.post({cmd:'SetValue',data:{uri:c.uri,values:value}});
+			}},
+			GM_getValue:{value:function(k,d){
+				var v=value[k];
+				if(v) {
+					k=v[0];
+					v=v.slice(1);
+					switch(k){
+						case 'n': d=Number(v);break;
+						case 'b': d=v=='true';break;
+						case 'o': try{d=JSON.parse(v);}catch(e){console.log(e);}break;
+						default: d=v;
 					}
-				}},
-				GM_addStyle:{value:function(css){
-					if(document.head) {
-						var v=document.createElement('style');
-						v.innerHTML=css;
-						document.head.appendChild(v);
-						return v;
+				}
+				return d;
+			}},
+			GM_listValues:{value:function(){return Object.getOwnPropertyNames(value);}},
+			GM_setValue:{value:function(key,val){
+				var t=(typeof val)[0];
+				switch(t){
+					case 'o':val=t+JSON.stringify(val);break;
+					default:val=t+val;
+				}
+				value[key]=val;comm.post({cmd:'SetValue',data:{uri:c.uri,values:value}});
+			}},
+			GM_getResourceText:{value:function(name){
+				var i,u;
+				for(i in resources) if(name==i) {
+					u=cache[resources[i]];
+					if(u) u=comm.utf8decode(window.atob(u));
+					return u;
+				}
+			}},
+			GM_getResourceURL:{value:function(name){
+				var i,u,r,j,b;
+				for(i in resources) if(name==i) {
+					i=resources[i];u=urls[i];
+					if(!u&&(r=cache[i])) {
+						r=window.atob(r);
+						b=new Uint8Array(r.length);
+						for(j=0;j<r.length;j++) b[j]=r.charCodeAt(j);
+						b=new Blob([b]);
+						urls[i]=u=URL.createObjectURL(b);
 					}
-				}},
-				GM_log:{value:function(d){console.log(d);}},
-				GM_openInTab:{value:function(url){
-					var a=document.createElement('a');
-					a.href=url;a.target='_blank';a.click();
-				}},
-				GM_registerMenuCommand:{value:function(cap,func,acc){
-					comm.command[cap]=func;comm.post({cmd:'RegisterMenu',data:[cap,acc]});
-				}},
-				GM_xmlhttpRequest:{value:function(details){
-					return Request(details);
-				}},
-			};
-			comm.forEach(g,function(i){var o=gf[i];if(o) addProperty(i,o,gm);});
-			return gm;
-		}
-		function run(l){while(l.length) buildCode(l.shift());}
+					return u;
+				}
+			}},
+			GM_addStyle:{value:function(css){
+				if(document.head) {
+					var v=document.createElement('style');
+					v.innerHTML=css;
+					document.head.appendChild(v);
+					return v;
+				}
+			}},
+			GM_log:{value:function(d){console.log(d);}},
+			GM_openInTab:{value:function(url){
+				// XXX: open from background to avoid being blocked
+				var a=document.createElement('a');
+				a.href=url;a.target='_blank';a.click();
+			}},
+			GM_registerMenuCommand:{value:function(cap,func,acc){
+				comm.command[cap]=func;comm.post({cmd:'RegisterMenu',data:[cap,acc]});
+			}},
+			GM_xmlhttpRequest:{value:function(details){
+				if(!comm.Request) comm.initRequest();
+				return comm.Request(details);
+			}},
+		};
+		comm.forEach(g,function(i){var o=gf[i];if(o) addProperty(i,o,gm);});
+		return gm;
+	},
+	loadScript:function(data){
 		function buildCode(c){
-			var req=c.meta.require||[],i,r=[],code=[],w=wrapGM(c),u;
-			comm.forEach(Object.getOwnPropertyNames(w),function(i){r.push(i+'=g["'+i+'"]');});
+			var req=c.meta.require||[],i,r=[],code=[],
+					wrapper=comm.wrapGM(c,data.values[c.uri],data.cache);
+			comm.forEach(Object.getOwnPropertyNames(wrapper),function(i){r.push(i+'=g["'+i+'"]');});
 			if(r.length) code.push('var '+r.join(',')+';delete g;with(this)!function(){');
-			for(i=0;i<req.length;i++) if(r=require[req[i]]) code.push(r);
+			for(i=0;i<req.length;i++) if(r=data.require[req[i]]) code.push(r);
 			code.push('!function(){'+c.code+'\n}.call(this)');	// to make 'use strict' work
 			code.push('}.call(this);');code=code.join('\n');
 			i=c.custom.name||c.meta.name||c.id;
-			if(o.injectMode==1) {	// advanced injection
-				u=comm.getUniqId();
-				comm.ainject[u]=[i,w];
-				comm.post({cmd:'Inject',data:[u,code]});
+			if(data.injectMode==1) {	// advanced injection
+				r=comm.getUniqId();
+				comm.ainject[r]=[i,wrapper];
+				comm.post({cmd:'Inject',data:[r,code]});
 			} else {	// normal injection
 				try{
 					r=new Function('g',code);
@@ -376,19 +383,20 @@ var comm={
 					console.log('Syntax error in script: '+i+'\n'+e.message);
 					return;
 				}
-				comm.runCode(i,r,w);
+				comm.runCode(i,r,wrapper);
 			}
 		}
+		function run(list){
+			while(list.length) buildCode(list.shift());
+		}
+		var start=[],idle=[],end=[],comm=this;
+		comm.command={};comm.ainject={};
 		comm.load=function(){run(end);run(idle);};
 		comm.checkLoad=function(){
 			if(!comm.state&&comm.inArray(['interactive','complete'],document.readyState)) comm.state=1;
 			if(comm.state) comm.load();
 		};
-
-		require=o.require;
-		cache=o.cache;
-		values=o.values;
-		comm.forEach(o.scripts,function(i,l){
+		comm.forEach(data.scripts,function(i,l){
 			if(i&&i.enabled) {
 				switch(i.custom['run-at']||i.meta['run-at']){
 					case 'document-start': l=start;break;

+ 7 - 6
src/style.less

@@ -10,11 +10,11 @@
 }
 html{
 	.fill;
-	body{
-		.fill;
-		background: #eee;
-		font: menu;
-	}
+}
+body{
+	.fill;
+	background: #eee;
+	font: menu;
 }
 h1{
 	text-shadow: #999 3px 3px 5px;
@@ -225,7 +225,8 @@ label>*{
 				color: blueviolet;
 			}
 			.icon{
-				opacity: 0.5;
+				//opacity: 0.5;
+				-webkit-filter: grayscale(.5);
 			}
 		}
 		&.moving{

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
src/style.min.css


Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio