Greasemonkey 㧠jQuery ã使ãããã®è¦ãæ¸ã
ãã¯ã jQuery ãªãã§ã¯ JavaScript ãæ¸ããªãç¶æ
ãªãã ãã©ãGreasemonkey ã§ä½¿ãããã«ã¯ä¸å·¥å¤«å¿
è¦ã ã£ãã®ã§ã¡ã¢ãã¦ããã
åèã«ããã¨ãã
- http://d.hatena.ne.jp/ysano2005/20060127/1138382734
- http://d.hatena.ne.jp/lurker/20060813/1155432961
æ¹é
- HTMLã«scriptã¿ã°ãåãè¾¼ã¾ãªãã
- ãã¼ã¸è¡¨ç¤ºæã«æ¯åãã¼ãããã®ã¯æéãªã®ã§ããã¼ã«ã«ã«ã¹ã¯ãªãããä¿åããã
- unsafeWindow.$ãããªãã¦ã$ã ãã§ä½¿ããããã«ããã
ã³ã¼ã
å®éã®ã³ã¼ãã¯ãããªæãã
loadJQueryãå¼ã³åºãã¨ããã¯ä»»æã®ã¤ãã³ãã§æ§ããªãã
(function() { const DEBUG = true; const KEY_JQUERY = "my_jquery"; var $ = "shortcut"; document.onload = function() { loadJQuery(initialize); } function loadJQuery(f_initialize) { if (eval('unsafeWindow.$')) {//ãã¼ãæ¸ã¿ãªãåæåå¦ç㸠f_initialize(); return; } var permanent = GM_getValue(KEY_JQUERY); if( typeof permanent == "undefined" ) {//ãã¼ã«ã«ã«ä¿åãã¦ãªããã°ãã¦ã³ãã¼ã GM_xmlhttpRequest({ method: 'get', url: 'http://jqueryjs.googlecode.com/files/jquery-1.1.3.1.js', onload: function(req){ GM_setValue( KEY_JQUERY, encodeURI(req.responseText) ); unsafeWindow.eval( req.responseText ); f_initialize(); }, onerror: function(req) { log("Request for jquery resulted in error code: " + req.status); } }); } else {//ãã¼ã«ã«ããåå¾ unsafeWindow.eval( decodeURI(permanent) ); f_initialize(); } } function log(message) { if (unsafeWindow && unsafeWindow.console && DEBUG) { unsafeWindow.console.log(message); } } function initialize() { $ = unsafeWindow.$; //æ¬æ¥ããããå¦çãæ¸ã } })();
注æç¹ã¨ã
ä¸ã®ã³ã¼ã㧠jQuery ã¯ä½¿ãããã©ããã¤ãéãã«ã¯ãããªãã
- jQuery.attrã使ããªããå¤ã®åå¾ãè¨å®ãã¨ã©ã¼ãçºçããã
- jQuery.valã§å¤ãè¨å®ãããã¨ãã¦ãåããåå¾ã¯ã§ããã
- $.ajaxã使ããªãï¼ä½ãèµ·ãããã¯å¿ããï¼ã
$.ajaxã¯GM_xmlhttpRequestã§ãããã ãã©ãjQuery.attrã¨jQuery.valã¯ããªãçãã確èªãã¦ãªããã©ãå¤åjQuery.textãåãã ããã
jQuery.attrã®æ¹ã¯ãnameå±æ§ã§å¯¾è±¡è¦ç´ ãåºå¥ãããã£ãã ããªã®ã§ãããªæãã§åé¿ããã
var target = $('.hoge[@name=fuga]');
jQuery.valã®æ¹ã¯ãjQuery.eachã®å¼æ°ã«DOMè¦ç´ ã渡ãããã®ãå©ç¨ããã
$('.hoge').each(function() { this.value = 'hogehoge'; });
ãã¨ãGreasemonkey ç¹æã§ã¯ãªããã©ãIDã§jQueryãªãã¸ã§ã¯ããåå¾ããå ´åã«å°ãå°ã£ãã
IDãéè¤ããå ´åãæåã«è¦ã¤ãã£ãDOMè¦ç´ ããè¿ã£ã¦ããªãã£ã½ããIDæ¬æ¥ã®æå³ããããã¨ãåä½çã«ã¯æ£ãããã ãã©ãã¦ãã¼ã¯ã«ãªã£ã¦ãªããã¼ã¸ãå¤ãã®ã§å°ã£ãã(Gmailã¨ãGmailã¨ãGmailã¨ãï¼
ãªã®ã§ã対象ã¨ãªãDOMè¦ç´ ãã¹ã¦ãå¦çãããå ´åã¯ãããªæãã«ããã
$('div[@id=duplicated]').each(function(){ alert($(this).text()); });