Advertisement
xlune

Jetpack Sample KBText

Apr 11th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Jetpack Sample KBText
  3.  * http://blog.xlune.com/
  4.  */
  5. var KBText = KBText || {};
  6. KBText.Config = {
  7.     yUrl: "http://jlp.yahooapis.jp/MAService/V1/parse",
  8.     yParam: {
  9.         appid: "rvNw382xg656tyPRhdp1t6_qfwWeQ1SWU_XEm.xaq8U8AIVo5bUDEgzeTlDqDr2TkZE-",
  10.         results: "ma",
  11.         sentence: ""
  12.     },
  13.     boxName: "KBTextBox"
  14. };
  15. KBText.init = function(){
  16.     var mes = "文字を選択してね!";
  17.     var selectStr = jetpack.tabs.focused.contentDocument.getSelection() || '';
  18.     if(selectStr)
  19.     {
  20.         mes = "変換開始!";
  21.         var param = KBText.Config.yParam;
  22.         param.sentence = selectStr;
  23.         $.post(
  24.             KBText.Config.yUrl,
  25.             param,
  26.             KBText.onComplete
  27.         );
  28.        
  29.     }
  30.     jetpack.notifications.show(
  31.         {
  32.             title: 'KBText',
  33.             body: mes,
  34.             icon: 'http://blog.xlune.com/favicon.ico'
  35.         }
  36.     );
  37. };
  38. KBText.onComplete = function(xml)
  39. {
  40.     var arr = [];
  41.     $(xml)
  42.         .find("reading")
  43.             .each(function(num){
  44.                 if(!$(this).text().match(/[  ]+/))
  45.                 {
  46.                     arr.push($(this).text());
  47.                 }
  48.             });
  49.     jetpack.notifications.show(
  50.         {
  51.             title: 'KBText',
  52.             body: "変換完了!",
  53.             icon: 'http://blog.xlune.com/favicon.ico'
  54.         }
  55.     );
  56.     KBText.showBox(arr.join(' ').cmabrigde());
  57. };
  58.  
  59. KBText.makeBox = function()
  60. {
  61.     return $("<div>")
  62.         .html('<p class="sj-text"></p><p class="sj-close"><a href="#">close</a></p>')
  63.         .css({
  64.                 "position": "fixed",
  65.                 "right": "5px",
  66.                 "top": "5px",
  67.                 "padding": "5px 10px",
  68.                 "background-color": "#000000",
  69.                 "color": "#FFFFFF",
  70.                 "z-index": "1000",
  71.                 "line-height": "1.5em",
  72.                 "width": "40%",
  73.                 "-moz-border-radius": "5px",
  74.                 "-moz-opacity": "0.7",
  75.                 "font-size": "14px"
  76.         })
  77.         .attr('id', KBText.Config.boxName)
  78.         .find('p.sj-close a')
  79.             .click(function(){
  80.                 $(jetpack.tabs.focused.contentDocument.documentElement)
  81.                     .find('#'+KBText.Config.boxName)
  82.                     .remove();
  83.                 return false;
  84.             })
  85.             .css({
  86.                 "display": "block",
  87.                 "text-align": "right",
  88.                 "color": "#FFFFFF"
  89.             })
  90.             .end();
  91. };
  92. KBText.showBox = function(str)
  93. {
  94.     KBText.removeBox();
  95.     $(jetpack.tabs.focused.contentDocument.documentElement)
  96.         .append(KBText.makeBox())
  97.         .find('p.sj-text').html(str);
  98. };
  99. KBText.removeBox = function()
  100. {
  101.     $(jetpack.tabs.focused.contentDocument.documentElement)
  102.         .find('#'+KBText.Config.boxName)
  103.         .remove();
  104. };
  105.  
  106. /**
  107.  * writed by "amachang"
  108.  * from: http://d.hatena.ne.jp/amachang/20090518/1242656425
  109.  */
  110. String.prototype.cmabrigde = function() {
  111.     return this.split(' ').map(function(w) {
  112.         var ws = w.split('');
  113.         var l = ws.pop();
  114.         return (ws.shift() || '') + (ws.length ? ws.sort(Math.random).join('') : '') + l;
  115.     }).join(' ');
  116. };
  117.  
  118. /**
  119.  * jetpack init
  120.  */
  121. jetpack.statusBar.append({
  122.     html: '<img src="http://blog.xlune.com/favicon.ico" />',
  123.     width: 16,
  124.     onReady: function(doc) {
  125.         $(doc).find("img").click(function() {
  126.             KBText.init();
  127.         });
  128.     }
  129. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement