diff -urN NinjaKit.safariextension/js/background.js /Users/misuzi/developing/NinjaKit.safariextension/js/background.js --- NinjaKit.safariextension/js/background.js 2011-11-18 01:16:33.000000000 +0800 +++ /Users/misuzi/developing/NinjaKit.safariextension/js/background.js 2011-12-10 01:48:23.000000000 +0800 @@ -79,8 +79,11 @@ } },false); safari.application.addEventListener("command", function(e){ - var tab = safari.application.activeBrowserWindow.openTab(); - tab.url = location.href.replace('background.html','options_page.html'); + if (e['command'] === 'NinjaKit') + { + var tab = safari.application.activeBrowserWindow.openTab(); + tab.url = location.href.replace('background.html','options_page.html'); + } }, false); } function handleMessage(request, sender, sendResponse){ @@ -183,7 +186,13 @@ responseText:'', responseHeaders:'' } - xhr.open(request.method || 'get', request.url, false); + xhr.open(request.method || 'get', request.url, true); + var reqHeaders=request.headers; + for(var i in reqHeaders) + { + xhr.setRequestHeader(i,reqHeaders[i]); + } + if (request.isOnload) { xhr.onload = function(){ Object.keys(res).forEach(function(k){ @@ -216,6 +225,27 @@ if (request.type === 'GM_setValue') { localStorage[request.namespace] = JSON.stringify(request.value); } + if (request.type === 'GM_registerMenuCommand') { + var id=request.id; + var callback=request.callback; + var title=request.title; + safari.application.addEventListener("contextmenu", handleContextMenu, false); + function handleContextMenu(event) { + event.contextMenu.appendContextMenuItem(id, title); + } + function performCommand(event) { + if (event.command === id) + { + sendResponse(id); + } + } + safari.application.addEventListener("command", performCommand, false); + } + if (request.type === 'GM_openInTab') { + var url=request.url; + var newTab=safari.application.activeBrowserWindow.openTab(); + newTab.url=url; + } } var meta_def = { "name":String, @@ -358,15 +388,35 @@ }); return js; } + function e4xToString(text){ } function gm_value(SCRIPT_NAME_SPACE){ - function GM_registerMenuCommand(caption, commandFunc/*, accelKey, accelModifiers, accessKey*/){ - var key = Date.now() + Math.random().toString(36); - document.addEventListener(caption+'.'+key,function(e){ - commandFunc(); - },false); - greasedChrome.commands.push({ns:SCRIPT_NAME_SPACE,caption:caption,key:key}); + function GM_registerMenuCommand(caption, commandFunc, accelKey, accelModifiers, accessKey){ + if(g.chrome) + { + var key = Date.now() + Math.random().toString(36); + document.addEventListener(caption+'.'+key,function(e){ + commandFunc(); + },false); + greasedChrome.commands.push({ns:SCRIPT_NAME_SPACE,caption:caption,key:key}); + } + else if(g.safari) + { + var opt={ + id: caption, + title:caption, + callback:!!commandFunc, + type:'GM_registerMenuCommand', + }; + sendRequest(opt,function(callback_id){ + if(callback_id===caption) + { + commandFunc(); + } + }); + } + } function GM_getValue(key, default_value){ if (key in __gm_value_store) { @@ -401,7 +451,7 @@ GM_setValue:GM_setValue, GM_registerMenuCommand:GM_registerMenuCommand, GM_deleteValue:GM_deleteValue, - GM_listValues:GM_listValues + GM_listValues:GM_listValues, }; } get_manifest(function(manifest){ diff -urN NinjaKit.safariextension/js/gm-wrapper.js /Users/misuzi/developing/NinjaKit.safariextension/js/gm-wrapper.js --- NinjaKit.safariextension/js/gm-wrapper.js 2011-11-18 01:16:33.000000000 +0800 +++ /Users/misuzi/developing/NinjaKit.safariextension/js/gm-wrapper.js 2011-12-10 01:48:23.000000000 +0800 @@ -173,12 +173,27 @@ GM_log = function GM_log(obj){ console.debug(obj); } + +GM_openInTab = function(url){ + console.debug(safari.application); + var _opt = { + type:'GM_openInTab', + url:url, + }; + sendRequest(_opt); +} 'forEach map filter some every reduce reduceRight indexOf lastIndexOf'.split(' ').forEach(function(m){ Array[m] = function(a,f,t){ return Array.prototype[m].call(a,f,t); } }); -unsafeWindow=window; +setTimeout(function() { + var div = document.createElement('div'); + div.setAttribute('onclick', 'return window;'); + unsafeWindow = div.onclick(); + unsafeWindow.sd=sendRequest; + }, 0); +//unsafeWindow=window; globalStorage = {wrappedJSObject:{}}; globalStorage.wrappedJSObject[document.domain] = localStorage; document.implementation.$createDocument = document.implementation.createDocument;