Jelajahi Sumber

sandbox fix and added start/stop hooks

Nathaniel van Diepen 10 tahun lalu
induk
melakukan
20b411a041
2 mengubah file dengan 25 tambahan dan 2 penghapusan
  1. 6 0
      app/www/data/plugins/test/script.js
  2. 19 2
      app/www/js/omnomirc.js

+ 6 - 0
app/www/data/plugins/test/script.js

@@ -5,4 +5,10 @@ hook('message',function(msg,from,room){
 });
 hook('send',function(msg,room){
 	return msg.toLowerCase()!='the game';
+});
+hook('start',function(){
+	$('body').show();
+});
+hook('stop',function(){
+	$('body').hide();
 });

+ 19 - 2
app/www/js/omnomirc.js

@@ -312,7 +312,7 @@
 		pluginSandbox = {
 			$: window.jQuery,
 			jQuery: window.jQuery,
-			$o: $o
+			$o: $o,
 		},
 		currentPlugin = 0,
 		Sandbox = function(sandbox){
@@ -349,7 +349,7 @@
 			args = exists(args)?args:[];
 			sandbox = sandbox instanceof Sandbox?sandbox:new Sandbox(sandbox);
 			var r = false;
-			fn = (fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,'');
+			fn = (fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,'').replace(/\\n/g,'\\\\n');
 			if(!exists(isFn) || !isFn){
 				fn = 'function(){'+fn+'}';
 			}
@@ -466,6 +466,21 @@
 						$o.plugin.stop(name);
 					}
 					event('Starting plugin '+name);
+					pluginSandbox.$o.hook = function(){
+						var h = arguments[0],
+							f = arguments[1],
+							fn;
+						if( h == 'start' || h == 'stop'){
+							fn = function(){
+								if(arguments[0] == name){
+									f.apply(this,arguments)
+								}
+							}
+						}else{
+							fn = f;
+						}
+						$o.hook.apply($o,[h,fn]);
+					};
 					if(!plugin.loaded){
 						$.ajax('data/plugins/'+name+'/script.js',{
 							dataType: 'text',
@@ -473,6 +488,7 @@
 								plugin.script = data;
 								runInSandbox(data,pluginSandbox);
 								plugin.started = true;
+								runHook('start',[name]);
 							}
 						});
 					}else{
@@ -487,6 +503,7 @@
 			stop: function(name){
 				if(exists(plugins[name])){
 					event('Stopping plugin '+name);
+					runHook('stop',[name]);
 					var i;
 					for(i in hooks){
 						if(hooks[i].type == 'plugin' && hooks[i].plugin == name){