Changeset 14764

Show
Ignore:
Timestamp:
06/28/08 17:00:33 (5 years ago)
Author:
gan2
Message:

branches/0.2 を trunk にマージ

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/userscripts/customPalletForHaiku/trunk/customPalletForHaiku.user.js

    r14643 r14764  
    33// @namespace      http://kojiran.googlepages.com/ 
    44// @include        http://h.hatena.ne.jp/* 
    5 // @version        0.14 
     5// @version        0.2 
    66// 
    7 // patched by gan2 
    8 // Idea for this script from lieutar 
     7// patched by gan2, lieutar. 
     8// Original userscript is written by MichaeL. 
     9// Idea for this script from lieutar. 
     10// 
     11// SEE ALSO: http://wedata.net/databases/hatena-CustomPalletForHaiku 
    912// 
    1013// ==/UserScript== 
    1114 
    12 location.href = 'javascript:(' + function() { 
    13   (function (w) { 
    14     if (w && w.Hatena && w.Hatena.Star) { 
    15       // 白("ffffff")は追加する必要なし 
    16       var pallets = { 
    17         'original': [ 
    18           "dedfde", "9ca2a5", "292829", 
    19           "de3039", "732c00", "f7b29c", "ffdf4a", 
    20           "7bbead", "295d52", "8cc7ef", "736dad" 
    21         ], 
    22         'kikuyamaru': [ 
    23           "000000", "eefd4c", "fef40c", "d6b331", 
    24           "9d5038", "f94a29", "dd001d", "91e71e", 
    25           "0c6c54", "2842bd", "2d366c", "1a1819", 
    26           "333333", "666666", "999999", "cccccc", 
    27           "fdd386", "ffcba6", "3e3725", "a700ad", 
    28           "356843", "1190b1", "4b00d4", "fed50c", 
    29           "fff381", "fd7622", "e85e66", "129e36", 
    30           "5f713a", "f9518d", "62c3e1" 
    31         ], 
    32         'gan2': [ 
    33           "dedfde", "c6c7c6", "9ca2a5", "63696b", "313431", "212421", // 黒 
    34           "ffcfce", "f7aaad", "e74d52", "de3039", // 赤 
    35           "b58a73", "9c6142", "732800", "4a2008", // 茶 
    36           "ffdfd6", "ffd3c6", "ffc3ad", "ffb29c", // 肌 
    37           "ffef94", "ffdf4a", // 黄 
    38           "f7b28c", "ef8242", "de7d4a", // 橙 
    39           "def3ef", "addbce", "7bbead", "316d63", "295d52", // 緑 
    40           "d6ebff", "8ccbef", "7b96ce", // 水 
    41           "b5aed6", "7b75b5" // 紫 
    42         ], 
    43         'wacom': [ 
    44           "f6d001", "9b3279", "d70067", "71a334", "016c9a" 
    45         ], 
    46         'dwacom': [ 
    47           "dedfde","9ca2a5","292829","de3039", 
    48           "732c00","f7b29c","ffdf4a","7bbead", 
    49           "295d52","8cc7ef","736dad","f6d001", 
    50           "9b3279","d70067","71a334","016c9a" 
    51         ] 
    52       }; 
    53  
    54       var entryForm = $X('.//form[@class="entry-form"]')[0]; 
    55       var textContainer = $X('div[@class="text-container"]', entryForm)[0]; 
    56       var colors = $N('span', {style: ["margin: 5px 5px"].join(';'), id: 'pallet-colors'}); 
    57       var changer = function () { 
    58         for (var i = 0, l = colors.childNodes.length; i < l; i++) { 
    59           colors.removeChild(colors.firstChild); 
    60         } 
    61         pallets[select.value].forEach(function (color) { 
    62           var cell = $N('span', {style: [ 
    63             "background: #" + color, 
    64             "padding: 2px 5px" 
    65           ].join(';')}); 
    66           colors.appendChild(cell); 
    67         }); 
    68         entryForm.insertBefore(colors, textContainer); 
    69       }; 
    70       var select = $N('select', { 
    71         name: 'pallets', 
    72         style: ["width: 300px", "margin: 2px 0px"].join(';')}); 
    73       select.addEventListener('change', changer, false); 
    74       for (var p in pallets) { 
    75         var opt = $N('option', {value: p}, p); 
    76         select.appendChild(opt); 
    77       } 
    78       entryForm.insertBefore(select, textContainer); 
    79       changer(); 
    80  
    81       SWFObject = function() { 
    82         deconcept.SWFObject.apply(this, arguments); 
    83         this.addVariable("colors", pallets[select.value]); 
    84         //select.style.display = 'none'; 
    85         //colors.style.display = 'none'; 
    86       }; 
    87       SWFObject.prototype = deconcept.SWFObject.prototype; 
    88        
    89       (function(orig){ 
    90         Hatena.Haiku.EntryForm.prototype.writeDrawForm = function(){ 
    91           if ( this.canvas ){ 
    92             var canvas = this.canvas; 
    93             var swf = canvas.getSWF(); 
    94             swf.parentNode.removeChild( swf ); 
    95             delete Hatena.Haiku.Canvas.canvases[canvas.canvasID]; 
    96           } 
    97           var retval = orig.apply(this, arguments); 
    98           var swf = this.canvas.getSWF(); 
    99           this.hasDrawForm = false; 
    100           return retval; 
     15(function(){ 
     16   
     17  if ( !(unsafeWindow && unsafeWindow.Hatena && unsafeWindow.Hatena.Star) ) 
     18    return; 
     19 
     20 
     21  unsafeWindow.CustomPalletForHaikuPallets = null; 
     22 
     23  var WEDATA_BASE  = 'http://wedata.net/databases/hatena-CustomPalletForHaiku'; 
     24  var WEDATA_FETCH = WEDATA_BASE + '/items.json' 
     25 
     26  // -------- 
     27  function log () { 
     28    var c = unsafeWindow.console; 
     29    if (c) { 
     30      c.log.apply(c, arguments); 
     31    } 
     32  } 
     33 
     34  //  
     35  var Wedata = (function(){ 
     36 
     37    function getHatenaID (){ 
     38      return unsafeWindow.Hatena.Visitor.name; 
     39    } 
     40 
     41    //TODO: implement me! 
     42    function put ( data ) { 
     43      throw new Error ( 'this operation is not supported still' ); 
     44    } 
     45 
     46    //TODO: implement me! 
     47    function modify ( data ) { 
     48      throw new Error ( 'this operation is not supported still' ); 
     49    } 
     50 
     51    return { 
     52      fetch: function(){ 
     53        GM_xmlhttpRequest({ 
     54          method: 'GET', 
     55          url:     WEDATA_FETCH, 
     56          onload: function(res) { 
     57            if ( res.status != 200 ){ 
     58              alert('something wrong...' + 
     59                    res.status + 
     60                    ' ' + 
     61                    res.statusText ); 
     62              return; 
     63            } 
     64            var error; 
     65            try { 
     66              var items = eval("( " + res.responseText + " )"); 
     67              if (!( items instanceof Array )) throw new Error; 
     68              var dic = {}; 
     69              items.forEach( function(item){ 
     70                dic[item.name] = item.data.colors.split(/\s+/); 
     71              }); 
     72            } catch ( error ) { 
     73              alert("received a malformed data." 
     74                    // + "\n" + error + "\n" + res.responseText 
     75                   ); 
     76              return; 
     77            } 
     78            var javascriptlet = 
     79              'javascript:void(window.CustomPalletForHaikuPallets=' + 
     80              dic.toSource() + ')'; 
     81            location.href = javascriptlet; 
     82          } 
     83        }); 
     84      }, 
     85 
     86      fix: function( name, pallet ){ 
     87        if( !confirm('really ??') ) return; 
     88        var data = { name: name , data: { colors: pallet } }; 
     89        if ( unsafeWindow.CustomPalletForHaikuPallets[name] instanceof Array ) { 
     90          modify( data ); 
     91        } else { 
     92          put( data ); 
     93        } 
     94      } 
     95    }; 
     96  })(); 
     97 
     98  Wedata.fetch(); 
     99 
     100  // Below script can not work with GM_* methods.. 
     101  // But work with real window object. 
     102  location.href = 'javascript:(' + function() { 
     103    (function (w) { 
     104 
     105      function log () { 
     106        var c = w.console; 
     107        if (c) { 
     108          c.log.apply(c, arguments); 
     109        } 
     110      } 
     111 
     112      function delayUntill( cond, action , max ){ 
     113        setTimeout( function(){ 
     114          if ( cond() ) { 
     115            action(); 
     116          } else if ( max > 0 ){ 
     117            delayUntill( cond , action , max - 1 ); 
     118          } 
     119        }, 10 ); 
     120      } 
     121 
     122      // initialize ... 
     123      function initSelector(){ 
     124        var entryForm     = $X('.//form[@class="entry-form"]')[0]; 
     125        var textContainer = $X('div[@class="text-container"]', entryForm)[0]; 
     126        var colors = $N('span', {style: ["margin: 5px 5px"].join(';'), 
     127          id: 'pallet-colors'}); 
     128        var changer = function () { 
     129          for (var i = 0, l = colors.childNodes.length; i < l; i++) { 
     130            colors.removeChild(colors.firstChild); 
     131          } 
     132          CustomPalletForHaikuPallets[select.value].forEach(function (color) { 
     133            var cell = $N('span', {style: [ 
     134              "background: #" + color, 
     135              "padding: 2px 5px" 
     136            ].join(';')}); 
     137            colors.appendChild(cell); 
     138          }); 
     139          entryForm.insertBefore(colors, textContainer); 
    101140        }; 
    102       })(Hatena.Haiku.EntryForm.prototype.writeDrawForm); 
    103     } 
    104  
    105  
    106     // -------- 
    107     function log () { 
    108       var c = w.console; 
    109       if (c) { 
    110         c.log.apply(c, arguments); 
    111       } 
    112     } 
    113  
    114     function $N(name, attr, childs) { 
    115       var ret = document.createElement(name); 
    116       for (var k in attr) if (attr.hasOwnProperty(k)) { 
    117         var v = attr[k]; 
    118         if (k == "class") ret.className = v; 
    119         else ret.setAttribute(k, v); 
    120       } 
    121       switch(typeof childs){ 
    122       case "string": 
    123         ret.appendChild(document.createTextNode(childs)); 
    124         break; 
    125       case "object": 
    126         for (var i=0, len=childs.length; i<len; i++) { 
    127           var child = childs[i]; 
    128           if (typeof child == "string") { 
    129             ret.appendChild(document.createTextNode(child)); 
    130           } else { 
    131             ret.appendChild(child); 
    132           } 
    133         } 
    134       } 
    135       return ret; 
    136     } 
    137  
    138     // $X 
    139     // based on: http://lowreal.net/blog/2007/11/17/1 
    140     // 
    141     // $X(exp); 
    142     // $X(exp, context); 
    143     // $X(exp, type); 
    144     // $X(exp, {context: context, 
    145     //          type: type, 
    146     //          namespace: {h:"http://www.w3.org/1999/xhtml"}}); 
    147     function $X (exp, context) { 
    148       var type, namespace={}; 
    149       // console.log(String(exp)); 
    150       if(typeof context == "function"){ 
    151         type = context; 
    152         context = null; 
    153       }else if(typeof context != "undefined" && !context['nodeType']){ 
    154         type = context['type']; 
    155         namespace = context['namespace'] || context['ns']; 
    156         context = context['context']; 
    157       } 
    158  
    159       if (!context) context = document; 
    160       var exp = (context.ownerDocument || context).createExpression(exp, function (prefix) { 
    161         return namespace[prefix] || 
    162           document.createNSResolver((context.ownerDocument == null ? context : context.ownerDocument) 
    163                                     .documentElement).lookupNamespaceURI(prefix) || 
    164           document.documentElement.namespaceURI; 
    165       }); 
    166  
    167       switch (type) { 
    168       case String: 
    169         return exp.evaluate( 
    170           context, 
    171           XPathResult.STRING_TYPE, 
    172           null 
    173         ).stringValue; 
    174       case Number: 
    175         return exp.evaluate( 
    176           context, 
    177           XPathResult.NUMBER_TYPE, 
    178           null 
    179         ).numberValue; 
    180       case Boolean: 
    181         return exp.evaluate( 
    182           context, 
    183           XPathResult.BOOLEAN_TYPE, 
    184           null 
    185         ).booleanValue; 
    186       case Array: 
    187         var result = exp.evaluate( 
    188           context, 
    189           XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
    190           null 
    191         ); 
    192         var ret = []; 
    193         for (var i = 0, len = result.snapshotLength; i < len; ret.push(result.snapshotItem(i++))); 
     141        var select = $N('select', { 
     142          name: 'pallets', 
     143          style: ["width: 300px", "margin: 2px 0px"].join(';')}); 
     144        select.addEventListener('change', changer, false); 
     145        for (var p in CustomPalletForHaikuPallets) { 
     146          var opt = $N('option', {value: p}, p); 
     147          select.appendChild(opt); 
     148        } 
     149 
     150        var $E = Ten.DOM.getElementsByTagAndClassName; 
     151        var textSelector = $E('img', 'input-selector-text')[0]; 
     152        new Ten.Observer(textSelector, 'onclick', function () { 
     153          select.style.display = 'inline'; 
     154          colors.style.display = 'inline'; 
     155        }); 
     156        var drawSelector = $E('img', 'input-selector-draw')[0]; 
     157        new Ten.Observer(drawSelector, 'onclick', function () { 
     158          select.style.display = 'none'; 
     159          colors.style.display = 'none'; 
     160        }); 
     161        var webcamSelector = $E('img', 'input-selector-webcam')[0]; 
     162        new Ten.Observer(webcamSelector, 'onclick', function () { 
     163          select.style.display = 'none'; 
     164          colors.style.display = 'none'; 
     165        }); 
     166 
     167        // add hooks for original Hatena objects. 
     168        SWFObject = function() { 
     169          deconcept.SWFObject.apply(this, arguments); 
     170          this.addVariable("colors", 
     171                           CustomPalletForHaikuPallets[select.value]); 
     172        }; 
     173        SWFObject.prototype = deconcept.SWFObject.prototype; 
     174         
     175        (function(orig){ 
     176          Hatena.Haiku.EntryForm.prototype.writeDrawForm = function(){ 
     177            if ( this.canvas ){ 
     178              var canvas = this.canvas; 
     179              var swf = canvas.getSWF(); 
     180              swf.parentNode.removeChild( swf ); 
     181              delete Hatena.Haiku.Canvas.canvases[canvas.canvasID]; 
     182            } 
     183            var retval = orig.apply(this, arguments); 
     184            var swf = this.canvas.getSWF(); 
     185            this.hasDrawForm = false; 
     186            return retval; 
     187          }; 
     188        })(Hatena.Haiku.EntryForm.prototype.writeDrawForm); 
     189 
     190        entryForm.insertBefore(select, textContainer); 
     191        changer(); 
     192      } 
     193 
     194      delayUntill( function(){ return CustomPalletForHaikuPallets } , 
     195                   initSelector, 100 ); 
     196 
     197 
     198 
     199      // utilities 
     200      function $N(name, attr, childs) { 
     201        var ret = document.createElement(name); 
     202        for (var k in attr) if (attr.hasOwnProperty(k)) { 
     203          var v = attr[k]; 
     204          if (k == "class") ret.className = v; 
     205          else ret.setAttribute(k, v); 
     206        } 
     207        switch(typeof childs){ 
     208        case "string": 
     209          ret.appendChild(document.createTextNode(childs)); 
     210          break; 
     211        case "object": 
     212          for (var i=0, len=childs.length; i<len; i++) { 
     213            var child = childs[i]; 
     214            if (typeof child == "string") { 
     215              ret.appendChild(document.createTextNode(child)); 
     216            } else { 
     217              ret.appendChild(child); 
     218            } 
     219          } 
     220        } 
    194221        return ret; 
    195       case undefined: 
    196         var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); 
    197         switch (result.resultType) { 
    198         case XPathResult.STRING_TYPE : return result.stringValue; 
    199         case XPathResult.NUMBER_TYPE : return result.numberValue; 
    200         case XPathResult.BOOLEAN_TYPE: return result.booleanValue; 
    201         case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: { 
    202           // not ensure the order. 
     222      } 
     223 
     224      // $X 
     225      // based on: http://lowreal.net/blog/2007/11/17/1 
     226      // 
     227      // $X(exp); 
     228      // $X(exp, context); 
     229      // $X(exp, type); 
     230      // $X(exp, {context: context, 
     231      //          type: type, 
     232      //          namespace: {h:"http://www.w3.org/1999/xhtml"}}); 
     233      function $X (exp, context) { 
     234        var type, namespace={}; 
     235        // console.log(String(exp)); 
     236        if(typeof context == "function"){ 
     237          type = context; 
     238          context = null; 
     239        }else if(typeof context != "undefined" && !context['nodeType']){ 
     240          type = context['type']; 
     241          namespace = context['namespace'] || context['ns']; 
     242          context = context['context']; 
     243        } 
     244 
     245        if (!context) context = document; 
     246        var exp = (context.ownerDocument || context).createExpression(exp, function (prefix) { 
     247          return namespace[prefix] || 
     248            document.createNSResolver((context.ownerDocument == null ? context : context.ownerDocument) 
     249                                      .documentElement).lookupNamespaceURI(prefix) || 
     250            document.documentElement.namespaceURI; 
     251        }); 
     252 
     253        switch (type) { 
     254        case String: 
     255          return exp.evaluate( 
     256            context, 
     257            XPathResult.STRING_TYPE, 
     258            null 
     259          ).stringValue; 
     260        case Number: 
     261          return exp.evaluate( 
     262            context, 
     263            XPathResult.NUMBER_TYPE, 
     264            null 
     265          ).numberValue; 
     266        case Boolean: 
     267          return exp.evaluate( 
     268            context, 
     269            XPathResult.BOOLEAN_TYPE, 
     270            null 
     271          ).booleanValue; 
     272        case Array: 
     273          var result = exp.evaluate( 
     274            context, 
     275            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
     276            null 
     277          ); 
    203278          var ret = []; 
    204           var i = null; 
    205           while (i = result.iterateNext()) { 
    206             ret.push(i); 
    207           } 
     279          for (var i = 0, len = result.snapshotLength; i < len; ret.push(result.snapshotItem(i++))); 
    208280          return ret; 
    209         } 
    210         } 
    211         return null; 
    212       default: 
    213         throw(TypeError("$X: specified type is not valid type.")); 
    214       } 
    215     } 
    216   }) (this.unsafeWindow || window); 
    217 }.toString() + ')()'; 
     281        case undefined: 
     282          var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); 
     283          switch (result.resultType) { 
     284          case XPathResult.STRING_TYPE : return result.stringValue; 
     285          case XPathResult.NUMBER_TYPE : return result.numberValue; 
     286          case XPathResult.BOOLEAN_TYPE: return result.booleanValue; 
     287          case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: { 
     288            // not ensure the order. 
     289            var ret = []; 
     290            var i = null; 
     291            while (i = result.iterateNext()) { 
     292              ret.push(i); 
     293            } 
     294            return ret; 
     295          } 
     296          } 
     297          return null; 
     298        default: 
     299          throw(TypeError("$X: specified type is not valid type.")); 
     300        } 
     301      } 
     302    }) (this.unsafeWindow || window); 
     303  }.toString() + ')()'; 
     304 
     305})();