Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

November 10, 2011

Google Scholarの検索結果に+1ボタンを追加するChrome Extension


Google Scholarの検索結果にはGoogle +1ボタンがついていないのでボタンを追加するChrome拡張機能をつくった.はじめは自分用に文献をソーシャルブックマークするシステムをApp Engineでつくろうかと思ったけどめんどくさいし,gdd11jpで+1ボタンの簡単だよって聞いたので.


インストール
Chrome Web Store - +1 to Google Scholar


構成

- manifest.json

- content_script.js


参考にしたページ

備考
  • +1ボタンの読み込み遅いせいか知らんけど結果の表示が遅くてイライラする…たぶんこいつのせいでGoogle Readerももたついてんじゃないの…
  • だからデフォルトで検索結果に+1ボタンつけてほしいです
  • しかし拡張機能のアイコンとかタイルとかGoogleのもの勝手に使ってるの怒られるのかな

February 8, 2010

JSONをHTMLのリスト形式で表示する

JSONをHTMLのリスト形式で表示するコードを書いてみた。

json = {"foo":"bar", "baz": [1,2,3]};

とかだったら

foo
bar
baz
  1. 1
  2. 2
  3. 3

以下、コード。

function json2list(json) {
 if (json==null) {
  return "null";
 }
 else if (json.constructor===Object) {
  var dl = $("<dl>");
  for (var key in json) {
   dl.append($("<dt>").html(key));
   dl.append($("<dd>").html(json2list(json[key])));
  }
  return dl;
 }
 else if (json.constructor===Array) {
  var ol = $("<ol>");
  var json_length = json.length;
  for (var i=0; i<json_length; i++) {
   ol.append($("<li>").html(json2list(json[i])));
  }
  return ol;
 }
 else {
  return json;
 }
}

January 24, 2010

Googleで優先する言語を保存できないのでGreasemokeyでなんとかした その2

相変わらず検索言語の設定が保存できないGoogle先生。MacのときのメインブラウザはSafariで検索は右上のSearchバーを使う。自分でつくっといてアレだが、Google Search Language Selector for Greasemonkey ではいちいち言語を選ぶのめんどくさい。

一発である言語で検索したい。のでまたGreasemonkeyのスクリプトを書いた。

Download

Google Search Language Jumper for Greasemonkey

Description

Googleで

Google en

って調べたら英語で「Google」を検索するページに飛ぶ。

Google ja

って調べたら日本語で「Google」を検索するページに飛ぶ。

January 16, 2010

Googleで優先する言語を保存できないのでGreasemokeyでなんとかした



Download

Google Search Language Selector for Greasemonkey

Description

Googleのデフォルトの言語は英語にしている。
でも日本語だけで調べたいときもある。

Setting > Search setting > Search language > Prefer pages written in these language(s):

で Japanese だけ選んでいれば日本語のページだけ検索できた。
のにちょっと前から勝手に

Search English and Japanese pages

になってしまう。
逆にデフォルトの言語を日本語にして

設定 > 検索設定 > 検索言語の設定 > 優先する言語:

で英語だけにチェックして保存しても

英語 と 日本語のページを検索

とかなっちゃう。
なんでだよ!ということでなんとかするためにGreasemonkeyのスクリプトを書いた。

Google Search Language Selector for Greasemonkey

デフォルトでは「すべての言語、英語、日本語」にしてあるけど、スクリプトをちょいと書き換えれば他の言語も単独で調べられるようになる。もうちょい書き換えれば、「英語とフランス語」とかいくつかの言語にも対応できる。

December 13, 2009

jQuery + PythonのJSONでのデータのやり取り


jQueryとPython CGIのデータのやり取りをJSONでおこないたい。
構成は、
index.html
sample.js
sample.py
(lib/jquery/jquery-1.3.2.js)

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>jQuery + Python</title>
    </head>
    <body>
        <h1>jQuery + Python</h1>
    
  <form id="hoge">
   <input type="text" name="foo" />
   <input type="text" name="bar" />
   <input type="text" name="baz" />
  </form>
  
  <a id="foo" href="#">show</a>
  <a id="bar" href="#">clear</a>
  <div id="baz"></div>

  <script type="text/javascript" src="lib/jquery/jquery-1.3.2.js"></script>
  <script type="text/javascript" src="sample.js"></script>
    </body>
</html>


sample.js

p2jはパラメータからJSON形式に変換する関数。どこかのページに掲載されていたのですが、失念してしまいました。すいません。
var $j = jQuery.noConflict();

function p2j(d) {
 if (d.constructor != Array) {
  return d;
 }
 var data={};
 for(var i=0;i<d.length;i++) {
  if (typeof data[d[i].name] != 'undefined') {
   if (data[d[i].name].constructor!= Array) {
    data[d[i].name]=[data[d[i].name],d[i].value];
   } else {
    data[d[i].name].push(d[i].value);
   }
  } else {
   data[d[i].name]=d[i].value;
  }
 }
 return data;
};


$j(document).ready(function(){ 
 $j('#foo')
 .click(function() {  
  // クエリ
  var query = $j(":input").serializeArray();
  console.log(p2j(query));
  
  // GETリクエスト
  $j.get('sample.py', query, function(text) {
   // 結果の処理
   var json = JSON.parse(text);
   var html = "";
   for (var i in json) {
    html += json[i].key + ':' + json[i].value + ' ';
   }
   $j('#baz').html(html);   
  });
  
  return false;
 });
 
 $j('#bar')
 .click(function (){
  $j('#baz').html('');
 }); 
});


sample.py

JSON形式にするにはjsonモジュールかsimplejsonモジュールがいいかもしれません。
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cgi
import cgitb; cgitb.enable()
import json

print "Content-type: text/javascript; charset=utf-8"
print

form = cgi.FieldStorage()

foo = form.getfirst("foo", "")
bar = form.getfirst("bar", "")
baz = form.getfirst("baz", "")

print json.dumps([{'key':'foo','value':foo},
                  {'key':'bar','value':bar},
                  {'key':'baz','value':baz}])

November 24, 2009

EclipseにAptanaプラグインを入れたのにjQueryのコード補完ができない


AptanaでjQueryのコードアシストしてくれない。はまった。

Grobal References(タブがないときはメニューバーから Window > Show View > References)でjQueryにチェックを入れたら解決。

November 21, 2009

LaTeX for BloggerをSafariと新しい編集インターフェイスのために改造する



Bloggerの新しいインターフェイスになってLaTeX for Bloggerが機能しない。(そもそもSafariだと微妙だった。)なのでちょっと書き換えた。正規表現を使ったり。チェックとかはしてない。

ダウンロード

LaTeX for Blogger w/ Undo in the Updated Editor

References

WolverineX02 - LaTeX for Blogger
http://wolverinex02.googlepages.com/emoticonsforblogger2

クリボウの Blogger Tips: Blogger に LaTeX 数式を挿入するユーザースクリプト「LaTeX for Blogger」更新
http://www.kuribo.info/2009/05/blogger-latex-latex-for-blogger.html


コード

// ==/UserScript== 以下を次のように書き換える。

function setlatex(domname) 
{
var editbar = document.getElementById(domname);
  if (editbar) {
    var latexbar = document.createElement('div');
    latexbar.setAttribute("id", "latexbar");
    latexbar.setAttribute("class", "goog-toolbar goog-toolbar-horizontal");

    var buttons = emoticonButton("Latex", "http://wolverinex02.googlepages.com/latex.gif");
    buttons += separator();
    buttons += emoticonButton2("Unlatex", "http://www.codecogs.com/gif.latex?Un%5CLaTeX");

    latexbar.innerHTML = buttons;
    editbar.appendChild(latexbar);
  }
}

function emoticonButton(name, url) {
    var button = " \
    <div class='goog-inline-block goog-toolbar-button' id='htmlbar_undefined' onmousedown=' \
    (function latex_compilator() { \
        var rich_edit = document.getElementById(\"postingComposeBox\"); \
        var rich_body = rich_edit.contentDocument.getElementsByTagName(\"body\"); \
        var contenu = rich_body[0].innerHTML; \
        contenu = contenu.replace(/\\$\\$(.+?)\\$\\$/g,\"<img src=\\\"http://www.codecogs.com/gif.latex?$1\\\" border=0 align=middle />\"); \
        rich_body[0].innerHTML = contenu; } \
    )();'> \
        <img src='" + url + "' alt='" + name + "' border='0'> \
    </div> \
    ";
 return button;
}

function emoticonButton2(name, url) {
    var button = " \
    <div class='goog-inline-block goog-toolbar-button' id='htmlbar_undefined' onmousedown=' \
    (function latex_decompilator() { \
        var rich_edit = document.getElementById(\"postingComposeBox\"); \
        var rich_body = rich_edit.contentDocument.getElementsByTagName(\"body\"); \
        var contentu = rich_body[0].innerHTML; \
        contentu = contentu.replace(/<img(.+?)gif.latex\\\?(.+?)\\\"(.*?)>/g, \"$$$$$2$$$$\"); \
        rich_body[0].innerHTML = contentu; \
    })();'> \
        <img src='" + url + "' alt='" + name + "' border='0'> \
    </div> \
    ";
 return button;
}

function separator() {
  return '<div class="goog-toolbar-separator goog-inline-block">&nbsp;</div>';
}

setlatex("postingComposeToolbar");


以下HTML編集モードに使うときのための自分用メモ

var html_edit = document.getElementById(\"postingHtmlBox\");
var contentu = html_edit.value;
...
html_edit = html_edit.value;
...
setlatex("postingHtmlToolbar");

August 13, 2009

はじめてのmixiアプリをGoogle App Engineにアップする

  1. Google App Engine(GAE)のアカウントをとる。
    mixiデベロッパー登録する。
  2. はじめてのmixiアプリの「Gadget XMLファイルの作成」に従いxmlをつくり、hello.xmlと名付ける。
  3. DeployするGAEフォルダ(テンプレだとapp.yaml、index.yaml、main.pyが入っている)にmixiというフォルダ(名前は任意)をつくり以下のような構成にする。

    ./app.yaml
    ./index.yaml
    ./main.py
    ./mixi/
    ./mixi/hello.xml


  4. app.yamlに設定を追加する。
    application: satomacoto
    version: 1
    runtime: python
    api_version: 1

    handlers:
      - url: /mixi
      static_dir: mixi

      - url: /.*
      script: main.py
  5. Deployする。たとえばアプリケーションを
      http://satomacoto.appspot.com/
    にDeployしたら
      http://satomacoto.appspot.com/mixi/hello.xml
    に存在するか確認する。
  6. 確認できたらはじめてのmixiのアプリ作成から手順通りに。

April 20, 2009

Save a Bookmark on Deliciousで自動的におすすめ/人気タグを入力するGreasemonkeyスクリプト

Download

Delicious Tagger for Greasemonkey

Code

// ==UserScript==
// @name        Delicious Tagger
// @namespace  http://satomacoto.blogspot.com/
// @include     http://delicious.com/save?*
// @description  Adds the populer/recommended tags
// ==/UserScript==

var d = document;

var recos = d.getElementById('save-reco-tags');
var pops = d.getElementById('save-pop-tags');

var tags = '';
if (recos != null) {
  recos = recos.getElementsByClassName('tag-list-tag')
  for (var i = 0; i < recos.length; i++)
    tags += recos[i].innerText + ' ';
}
if (pops != null) {
  pops = pops.getElementsByClassName('tag-list-tag');
  for (var i = 0; i < pops.length; i++)
    tags += pops[i].innerText + ' ';
}

d.getElementById('tags').value = tags;
d.getElementById('tags').focus();

YouTubeで自動的に高画質動画に飛ぶGreasemonkeyスクリプト

// ==UserScript==
// @name        Jump to fmt=18
// @namespace  http://satomacoto.blogspot.com/
// @include     http://*youtube.com/watch*
// @description  YouTubeで自動的に高画質動画(fmt=18)に飛ぶ
// ==/UserScript==

(function() {
var url = location.href;
if(url.indexOf("&fmt")<0){
location.replace(url + "&fmt=18");
}
})();