ã¯ã©ã¤ã¢ã³ããµã¤ãWebä»ç®ç´ Greasemonkey
ã°ãªã¢ã³ã§ãããªã®ä½ã£ã¦ã¿ã¾ããã
ããã¯ä½ï¼
Greasemonkeyã使ã£ãWebä»ç®ç´ãã¼ã«ã§ãã
Webä¸ã®ä»»æã®ãã¼ã¸ã«ã¡ã¢ãè²¼ãä»ãã¦ãããã¨ãã§ãã¾ãã
Greasemonkeyã®GM_getValue/GM_setValueã使ã£ã¦ãã¼ã¿ãã¯ã©ã¤ã¢ã³ããµã¤ãã«ä¿åãã¾ãã
ãã®ãããã°ã¤ã³ä¸è¦ã§ä½¿ãã¾ããã©ãè¤æ°ç«¯æ«ã§å
±æããããä»ã®äººã«è¦ãã¦ããããã¯ã§ãã¾ããã
ä½æåæ©
ã¾ããã¨JavaScript & Ajax ! Vol.1ã«è¼ã£ã¦ããFirefox2.0ã®ã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã使ã£ã¦ä½ãã§ããªãããªã¨æã£ã¦ä½ãã¾ãããæåãæ®éã®<script>ã¿ã°ã§ã¹ã¯ãªãããèªã¿è¾¼ã¾ãã¦ä½ã£ã¦*1ã¡ããã¨åããã®ã§ãããGreasemonkeyã®user.jsã¨ãã¦åããããããªããã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ãèªã¿è¾¼ãã*2ãGM_setValue/GM_getValueã¨ããã®ãç¥ã£ã¦ããã£ã¡ã使ãããã«åãæ¿ãã¾ããã
ã¤ã³ã¹ãã¼ã«
http://sawat.jf.land.to/gm/GmSticky.user.jsãã¤ã³ã¹ãã¼ã«
使ãæ¹
- ä»»æã®ãã¼ã¸ã§ Altãã¼ãæ¼ããªãã @ãã¼ãæ¼ãã¨æ°ããä»ç®ç´ãè²¼ãä»ãããã¨ãã§ãã¾ãã
- 表示ããããã¤ã¢ãã°ã«å 容ãè¨å ¥ãã¾ãã(å³2)
- åæå¤ã¯ç¾å¨ã®æ¥æã§ãã*3
- "<"ããã¯ãããã¨HTMLã¿ã°ãæå¹ã«ãªãã¾ãã
- HTMLã¿ã°ã使ããªãå ´åã¯ã\nã§æ¹è¡ã§ãã¾ãã
- è²¼ãä»ããä»ç®ç´ãããã«ã¯ãªãã¯ããã¨ç·¨éã§ãã¾ãã(å³3)
- "<"ããã¯ãããã¨HTMLã¿ã°ãæå¹ã«ãªãã¾ãã
- HTMLã¿ã°ã使ããªãå ´åã¯ã\nã§æ¹è¡ã§ãã¾ãã
- 空ã«ãã¦ãOKãããã¨ä»ç®ç´ãåé¤ããã¾ãã
- REMOVE_ALLã¨è¨å ¥ããã¨ããã®ãã¼ã¸ã®ä»ç®ç´ãå ¨ã¦åé¤ãã¾ãã
- è²¼ãä»ããä»ç®ç´ã¯ãã¦ã¹ã§ãã©ãã°ãã¦ç§»åããããã¨ãã§ãã¾ãã
- è²¼ãä»ããä»ç®ç´ãShiftãã¼ãæ¼ããªããã¯ãªãã¯ããã¨åé¤ã§ãã¾ãã
- è²¼ãä»ããä»ç®ç´ãCtrlãã¼ãæ¼ããªããã¯ãªãã¯ããã¨è¤è£½ã§ãã¾ãã
- ä»ç®ç´ã¯ãã¼ã¸ãã¨*4ã«ç®¡çããã¾ããåé¤ããªãéãæ®ãã¾ãã
- äºåº¦ã¨è¡ããªããã¼ã¸ããã©ã³ãã å¤ãã»ãã·ã§ã³IDãå«ãURLã®ãã¼ã¸ã«ã¯è²¼ããªãããã«ã
- about:configã§'sticky'ã§ãã£ã«ã¿ãæããã°ä»ç®ç´ã®ãã¼ã¿ãè¦ãã¾ãã
- çè·¡ãå®å ¨ã«åé¤ããã«ã¯ã表示ãããé ç®ãããªã»ããããã¦ãã ããã(è¿½è¨ 2/28)(ä¸ã®è¿½è¨ãåç §ãã¦ãã ãã)
ã¯ã¾ã£ãã¨ãããªã©
- ã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã«ã¯æååããä¿åã§ããªãã
- JSONå½¢å¼ã§ä¿åããã°ããã
- ã°ãªã¢ã³ããã¯ã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã«ã¢ã¯ã»ã¹ã§ããªãã
- GM_setValue/GM_getValueã§æ¥æ¬èªãå
¥ããã¨æååãããã
- encodeURIã¨decodeURIã§è§£æ±ºã
ããããã¨æã£ããã©ãããªãã£ãæ©è½
- prompté¢æ°ãããªãå
¥åãã©ã¼ã
- é¢åèãã®ã§å´ä¸ã
- ç·¨éãã¿ã³ã»åé¤ãã¿ã³ã»æ¨ªå¹
å¤æ´ãã¿ã³
- é¢åèãããç¡ãã¦ãåé¡ãªãã®ã§å´ä¸ã
- ä»ç®ç´ã®æå¹ã¹ã³ã¼ãã®è¤æ°å (åä¸ãã¡ã¤ã³å
ã§å¸¸ã«è¡¨ç¤ºãªã©)
- å¤åã使ãéãç¡ãã®ã§å´ä¸ã
- åä¸ãã¡ã¤ã³å ã®ä»ç®ç´ãä¸æ¬åé¤
- Firefox以å¤ã®ãã©ã¦ã¶ã¸ã®å¯¾å¿
- IE/Trixieã¯Young risk taker.: [javascript] IEとFirefox2.0以上で動作するClient Side Storageã使ãã°ã§ãããããã§ããããªãã
- Safari/Creammonkeyã¯Macæã£ã¦ãªãããOperaã¯globalStorageãªããã
- ã¨ãããããããã¨ã¯æããªãã£ãã
ã½ã¼ã¹
// ==UserScript== // @name GmSticky // @namespace http://d.hatena.ne.jp/sawat/ // @description Make stikies and save at greasemonkey storage. // @include http://* // @include https://* // ==/UserScript== if(window.Sticky == void(0)) { (function() { var Util = { document: window.document, // DOMæ§ç¯é¢é£ createElement: function(tagName, parent, style, attributes, listeners, innerHTML) { var element = Util.document.createElement(tagName); if(attributes) {Util.setAttributes(element, attributes); } if(parent) {parent.appendChild(element);} if(style) {Util.setStyle(element, style); } if(innerHTML) {element.innerHTML = innerHTML;} if(listeners) {Util.setListeners(element, listeners); } return element; }, setAttributes : function(element, attributes) { for(var prop in attributes) { element.setAttribute(prop, attributes[prop]); } }, setListeners : function(element, listeners) { var func = element.addEventListener ? function(name, listener) { element.addEventListener(name, listener, false); } : function(name, listener) { element.attachEvent('on' + name, listener); } ; for(var prop in listeners) { func(prop, listeners[prop]); } }, setStyle : function(element, style) { Util.marge(element.style, style); }, marge : function(dest, src) { for(var prop in src) { dest[prop] = src[prop]; } }, escapeHtmlChar : function(str) { return str && str.replace(/</g, '<').replace(/>/g, '>').replace(/\\n/g, '<br />'); } }; /** Sticky ã³ã³ã¹ãã©ã¯ã¿. */ window.Sticky = function Sticky(text, x, y) { this.text = text; this.div = Util.createElement('div', document.body, Sticky.STYLE, null, this.createListeners(), null); this.clickX = 0; this.clickY = 0; this.edit(text); this.setPosition(x || document.body.scrollLeft + Math.floor(document.body.clientWidth/2), y || document.body.scrollTop + Math.floor(document.body.clientHeight/2)); }; /** ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ãã */ window.Sticky.prototype = { /** ç·¨é */ edit: function(newText) { newText = newText || prompt(Sticky.EDIT_MESSAGE, this.text); // ãã£ã³ã»ã« if(newText == void(0)) return; // åé¤ if(newText == '') { this.dispose(); return; } // ãã¼ã¸å åé¤ if(newText == 'REMOVE_ALL') { Sticky.removeAll(); return; } // ãã¡ã¤ã³å åé¤ if(newText == '!REMOVE_ALL') { Sticky.removeAllInDomain(); return; } var use_html = newText.match(/^</) != null; try { this.div.innerHTML = use_html ? newText : Util.escapeHtmlChar(newText); } catch(e) { this.div.innerHTML = Util.escapeHtmlChar(newText); } this.text = newText; Sticky.store(); }, /** ç ´æ£ */ dispose : function(flag) { this.div.parentNode.removeChild(this.div); if(!flag) Sticky.remove(this); }, /** é ç½® */ setPosition : function(x, y) { this.div.style.left = Math.max(10, Math.min(document.body.scrollWidth - this.div.clientWidth - 10, x - this.clickX)) + "px"; this.div.style.top = Math.max(10, Math.min(document.body.scrollHeight - this.div.clientHeight - 10, y - this.clickY)) + "px"; }, /** ã³ãã¼ */ copy : function () { return Sticky.create(this.text, parseInt(this.div.style.left)+10, parseInt(this.div.style.top)+10); }, /** åå¥ã®ä»ç®ç´(div)ã«å¯¾ãããªã¹ãã¼ */ createListeners : function() { var sticky = this; return { /** ããã«ã¯ãªã㯠: ç·¨é */ 'dblclick' : function(event) { sticky.edit(); Sticky.moving = null; }, /** ãã¦ã¹ãã¦ã³ : ãã©ãã°éå§ */ 'mousedown': function(event) { Sticky.moving = sticky ; sticky.clickX = event.pageX - parseInt(sticky.div.style.left); sticky.clickY = event.pageY - parseInt(sticky.div.style.top); }, /** Shift+ã¯ãªã㯠: åé¤ Ctrl+ã¯ãªã㯠: ã³ãã¼ */ 'click' : function (event) { if(event.shiftKey) sticky.dispose(); if(event.ctrlKey) sticky.copy(); } }; }, /** æååå(JSON) */ toString: function() { return '{ x:' + parseInt(this.div.style.left) + ',y:' + parseInt(this.div.style.top) + ',text:"' + this.text.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"' + // " '}'; } } /** ã¯ã©ã¹ã®ã¡ã½ããã»å®æ°(?) */ Util.marge(window.Sticky, { _storage: [], /** æ°è¦ä½æ */ create: function(text, x, y, use_html) { text = text || prompt('New Sticky', document.getSelection() || new Date().toLocaleString()); if(!text) return; Sticky.add(new Sticky(text, x, y, use_html)); Sticky.store(); }, /** ç»é² */ add: function(newSticky) { Sticky._storage.push(newSticky); }, /** åé¤ */ remove: function(sticky) { for(var i=0,n=Sticky._storage.length; i<n; i++) { if(Sticky._storage[i] == sticky) { Sticky._storage.splice(i, 1); break; } } Sticky.store(); }, /** ãã¼ã¸å å ¨åé¤ */ removeAll: function(sticky) { for(var i=0,n=Sticky._storage.length; i<n; i++) { Sticky._storage[i].dispose(); } Sticky._storage = []; Sticky.store(); }, /** ãã¡ã¤ã³å å ¨åé¤ */ removeAllInDomain: function(sticky) { Sticky.removeAll(); return; }, /** ã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã¸ã®ä¿åãã¼ä½æ */ key : function() { return '_stickies@' + document.location.host + document.location.pathname + document.location.search; }, /** ã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ããã®èªã¿è¾¼ã¿ */ load : function() { var data = GM_getValue(Sticky.key()); if(!data) return; try { var stickies = eval(decodeURI(data)); for(var i=0; i<stickies.length; i++) { Sticky.create(stickies[i].text, stickies[i].x, stickies[i].y); } } catch(e) { new Sticky('<span style="color:red;font-weight:bold">Error on load stickise data.</span>', 1, 1); } }, /** ã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã¸ã®æ¸ã込㿠*/ store : function() { var data = '[' + Sticky._storage.join(',') + ']'; GM_setValue(Sticky.key(), encodeURI(data)); }, /** åæå */ initialize : function () { Util.setListeners(document.getElementsByTagName('html')[0], Sticky.GLOBAL_LISTENER); setTimeout(Sticky.load, 500); }, /** ä»ç®ç´ã®ã¹ã¿ã¤ã« */ STYLE : { position: 'absolute', left: '0px', top: '0px', cursor: 'move', minimumWidth: '100px', borderColor: '#663333', borderWidth: '1px 2px 2px 1px', borderStyle: 'solid', backgroundColor: '#FFFFEF', fontSize: '90%' }, /** documentå ¨ä½ã«å¯¾ãããªã¹ãã¼ */ GLOBAL_LISTENER : { /** ãã¦ã¹ã ã¼ã : ãã©ãã° */ 'mousemove' : function (event) { if (!Sticky.moving) return; Sticky.moving.setPosition(event.pageX, event.pageY); }, /** ãã¦ã¹ã¢ãã : ãã©ãã°çµäº */ 'mouseup' : function (event) { if (!Sticky.moving) return; Sticky.moving.clickX = 0; Sticky.moving.clickY = 0; Sticky.moving = null; Sticky.store(); }, /** æ°è¦ä½æ */ 'keydown' : function (event) { if (event.altKey && event.keyCode == 192 /* @ãã¼ */) { Sticky.create(); } } }, /** ç·¨éãã¤ã¢ãã°ã¡ãã»ã¼ã¸ */ EDIT_MESSAGE : 'Edit\n'+ ' Start with "<" : Use HTML tags.\n' + ' Empty : Remove this sticky\n' + ' "REMOVE_ALL" : Remove all stickies in this page.\n', VERSION : '0.01' }); Sticky.initialize(); })(); }
追è¨(2/28)
GM_setValueã§è¨å®ããå¤ãåé¤ããããã®é¢æ°ãGreasemonkeyã«ãªããããããããªãã¼ã¸ã«ä»ç®ãè²¼ã£ããæ¶ããããã¦ããã¨æ¬¡ç¬¬ã«about:configã®è¨å®ãå¢ãã¦ãã¾ãã¾ãããã®ãããæã
about:config ãéã㦠sticky ã§ãã£ã«ã¿ãæãã¦åºã¦ããé
ç®ããªã»ãããããã¨ããªã¹ã¹ã¡ãã¾ã(å³4)ã
ãªã»ãããããé
ç®ã¯Firefoxãåèµ·åããã¨å®å
¨ã«æ¶å»ããã¾ãããã ããåèµ·ååã«è¨å®ãåé¤ãããã¼ã¸ã表示ããã¨GM_getValueã«å¤±æãã¦ã¨ã©ã¼ãçºçãã¾ãã
Firefoxãèµ·åãã¦ããªãã¨ãã« prefs.js *5ãç·¨éãã¦åé¤ãã¦ããã¾ãã¾ããã
*1:user.jsã ã¨Firebugã使ããªãã®ã§â¦ã
*2:å°ç´°å·¥ãããã°èªã¿è¾¼ãããã¨ã¯ããã£ãã®ã§ãããGM_setValue/GM_getValueã«æ¯ã¹ã¦ã¡ãªãããç¡ãã£ãã®ã§ã
*3:æ¥æã®ãã©ã¼ããããã¢ããªã®ã¯Date.prototype.toLocaleStringã®ããã§ããå¹´ææ¥ãæ¼¢åã§ãæåç§ãã³ãã³ã¦ï½¥ï½¥ï½¥ã
*4:ãã¹ãåã¨ãã¹ã¨ã¯ã¨ãªã¼æååã§èå¥ãã¾ãã
*5:prefs.jsã®ããã㯠http://www.mozilla-japan.org/support/firefox/edit#profile ãåç §