ã°ãªã¢ã³userscriptãChromeã«ç§»æ¤ããã¨ãã«ä½¿ããGM APIã®ã©ããã£ã½ããã®ã¨ããã¸ã§ã¯ããã³ãã¬ã¼ããæ¸ãã
2010/10/17 å
¬éå
ãGithubã«ãã¦ãã¾ãã
http://d.hatena.ne.jp/umezo/20100411/1270988391
Chromeéãã¦è¯ãã®ã§ãããããããããã°ãªã¢ã³ã¨ã便å©ãªç©ãç¡ãã
ããã§ã°ãªã¢ã³ã®ã¹ã¯ãªãããextensionã¨ãã¦Chromeã«æã£ã¦è¡ãããã®ã§ãããGM_*ãªAPIãç¡ãâ¦â¦
ããã«ãextensionã®å¶éä¸ã®çç±ãããå®å
¨ã«ç§»æ¤ãããã¨ã¯åºæ¥ã¾ããã
ãã ããä½ç¹ãã®å¦¥åã¨æ¢åã®user scriptã®ã¡ãã£ã¨ããä¿®æ£ã§ç§»æ¤å¯è½ã§ãã
移æ¤ç¨ã«å¯è½ãªéãGM_*ãã©ããåºæ¥ããããªãã®ãä½ãã¾ããã
http://umezo.tsuyabu.in/chrome/gmWrapper.zip
使ãæ¹
zipãã¡ã¤ã«ãå±éããã¨ä»¥ä¸ã®ãããªãã¡ã¤ã«ãæ ¼ç´ããã¦ãã¾ãã
manifest.jsonã移æ¤ããã°ãªã¢ã³ã«åããã¦ç·¨éãã¦ãã ããã
youGMScript.user.jsã¯ãµã³ãã«ã§ãã移æ¤ãããuserscriptã«ç½®ãæããåããã¦ãããã§ã¹ããä¿®æ£ãã¦ãã ããã
background.html background.js gmWrapper.js manifest.json yourGMScript.user.js
ãããã§ã¹ãã®å¤æ´ç®æ
ãããã§ã¹ãã®ä¸èº«
1 { 2 "name": "change name for your GM script", 3 "version": "1.0", 4 "description": "change description for your GM script", 5 "permissions" : [ 6 "http://*/*" , 7 "https://*/*" 8 ] , 9 "background_page": "background.html", 10 11 "content_scripts": [{ 12 "matches": [ 13 "http://*/*" , 14 "https://*/*" 15 ], 16 "js": [ "gmWrapper.js" , "yourGMScript.user.js" ] 17 }] 18 }
background_pageã¨content_scripts.js[0]ã®gmWrapper.js以å¤ãuserscriptã«åããã¦ä¿®æ£ãã¦ãã ããã
userscriptã®ä¿®æ£
GM APIã使ã£ã¦ããå ´åã¯ã©ããã¦ãå¸åããããªãã£ãã®ã§ãå梱ã®yourGMScript.user.jsãåèã«ä¿®æ£ãå ãã¦ãã ããã
yourGMScript.user.jsä¸èº«
1 // ==UserScript== 6 // ==/UserScript== 10 11 if( typeof isChromeExtension == "undefined" ){ //chrome wrapperãèªã¿è¾¼ã¾ãã¦ããªãå ´å = ã°ãªã¢ã³ã«èªã¿è¾¼ã¾ãã¦ãã 12 init(); 13 }else{ //ãã£ã¡ã®å ´åã¯ã©ããã¼ã¨ä¸ç·ã«èªã¿è¾¼ã¾ãã¦ããã®ã§onReadyGMã¤ãã³ããåãåã£ã¦ããåæå 14 function onReadyGM(){ 15 init(); 16 } 17 } 18 //userscriptã®åæåå¦ç 19 function init(){ 20 GM_setValue( "key1" , "value1" ); 21 GM_log( GM_getValue( "key1" ) ); 22 23 GM_log( GM_getValue( "key2" , "default2" ) ); 24 GM_setValue( "key2" , "value2_stored" ) ; 25 26 GM_setValue( "key1" , "value1_new" ); 27 GM_log( GM_getValue( "key1" ) ); 28 30 GM_xmlhttpRequest({ 31 method : "get" , 32 url: "http://www.google.com/" , 33 onload : function (req) { 34 GM_log( req ); 35 } 41 }); 42 }
ã©ããåºæ¥ãªãã£ãAPI
GM_registerMenuCommandã¯ã²ã¨ã¾ã諦ãã¾ããã
ããç¡çç¢ç移æ¤ããã¨ããã°page_actionãå©ç¨ããå½¢ã«ãªãã¨æãã¾ãã
ã¾ã¨ã
ã¨ã£ã¨ã¨ãã°ãªã¢ã³ãã®ãã®ã移æ¤åºæ¥ããããAPIãå å®ããã¨ããã§ããã
以ä¸ä»é²
GM_getValue,GM_setValueã®ç§»æ¤æ¹æ³
ãã¼ã¿ã®ä¿åå ´æã¯ãã¡ããlocalStorageã使ãã¾ãã
ãã®ãããbackground_pageã«localStorageã¸ã¢ã¯ã»ã¹ããAPIããããã¾ãã
ã ããããã¼ã¿ã®åå¾ã®ãã³ã«background_pageã«åãåããããã¨ãéåæã«ããªãã¦ã¯ãããªãã®ã§ã
åæåå¦çã®ã¨ãã«localStorageã丸ãã¨åãåã£ã¦ãã£ãã·ã¥ãã¾ãã
æ¸å¿µã¨ãã¦ã¯localStorageã«ãã®ããã巨大ãªç©ãä¿åãããããªå ´åã§ãããã»ã»ã»ã
ãããªã¹ã¯ãªãããããã§ããããã»ã»ã»ã
ã©ããã¼é¨ã®ã³ã¼ã
ãåç¥ã 33 //background_pageããlocalStorageãåãåã£ã¦ãã£ãã·ã¥ 34 //onReadyGMãã³ãã©ãå®è¡ 35 function onInitializedGM( response ){ 36 localStorage = response ; 37 isInitialized = true ; 38 onReadyGM(); 39 } ãä¸ç¥ã 49 GM_setValue = function ( key , value ){ 50 if( !isInitialized ){ 51 console.log( "Error" , "GM_setValue was called before finished initializing" ); 52 return ; 53 } 54 55 //ãã£ãã·ã¥ã«æ¸ã込㿠56 localStorage[ key ] = value ; 57 58 //background_pageã«æ¸ãè¾¼ã¿ã¡ãã»ã¼ã¸éä¿¡ 59 port.postMessage( { action : "setValue" , args : [ key , value ] } ); 60 }; 61 62 63 GM_getValue = function ( key , def ){ 64 if( !isInitialized ){ 65 console.log( "Error" , "GM_getValue was called before finished initializing" ); 66 return ; 67 } 68 69 //å¤ãã»ããããã¦ããªããã°APIçµç±ã§ä¿å 70 if( localStorage[ key ] == undefined && def != undefined ){ 71 GM_setValue( key , def ); 72 } 73 74 //ãã£ãã·ã¥ããèªã¿è¾¼ãã§è¿ã 75 return localStorage[ key ] ; 76 };
GM_xmlhttpRequest
以åæ¸ããã¨ã³ããªã¼ã®ã¢ã¤ãã¢ããã®ã¾ã¾ä½¿ã£ã¦ãã¾ãã
http://d.hatena.ne.jp/umezo/20091114/1258193365
GM_log
consoe.log
GM_registerMenuCommand
ã²ã¨ã¾ãä¿ç