自作のGoogle chrome extensionを作ってみたので、とりあえずソースを公開してみる
とりあえず公開。後日説明などを書き足します。
作ったもの
- twitter.comで使えるextension
- アルファベットを回転してポストする(pipe1)
- 英訳・和訳してポストする(pipe2)
exntensionの残タスク
- このextensionのアーキ整理・説明
- リファクタリング
- 機能・アーキ拡張
source code of pipe1
manifest.json
{ "name": "pipe for twitter", "version": "1.0", "description": "tweets will be inverted.", "content_scripts": [ { "matches": [ "http://twitter.com/*" ], "js": [ "content.js" ] } ] }
content.js
function convert( ) { var str = document.getElementById( "status" ).value ; document.getElementById( "status" ).value = flip( str ) ; } function onClick( e ) { e.preventDefault( ) ; e.stopPropagation( ) ; if( document.getElementById( "status" ).value == "" ) return false ; convert( ) ; document.getElementById("status_update_form").submit( ) ; } document.getElementById("tweeting_button").addEventListener( 'click', onClick, true ) ; function flip( str ) { return result = flipString( str ); } function flipString(aString) { var last = aString.length - 1; //Thanks to Brook Monroe for the //suggestion to use Array.join var result = new Array(aString.length) for (var i = last; i >= 0; --i) { var c = aString.charAt(i) var r = flipTable[c] result[last - i] = r != undefined ? r : c } return result.join('') } var flipTable = { a : '\u0250', b : 'q', c : '\u0254', //open o -- from pne d : 'p', e : '\u01DD', f : '\u025F', //from pne g : '\u0183', h : '\u0265', i : '\u0131', //from pne j : '\u027E', k : '\u029E', //l : '\u0283', m : '\u026F', n : 'u', r : '\u0279', t : '\u0287', v : '\u028C', w : '\u028D', y : '\u028E', '.' : '\u02D9', '[' : ']', '(' : ')', '{' : '}', '?' : '\u00BF', //from pne '!' : '\u00A1', "\'" : ',', '<' : '>', '_' : '\u203E', ';' : '\u061B', '\u203F' : '\u2040', '\u2045' : '\u2046', '\u2234' : '\u2235', '\r' : '\n' //thank you, Yeeliberto } for (i in flipTable) { flipTable[flipTable[i]] = i }
ソースの大半はここより拝借。
source code of pipe2
manifest.json
{ "name": "pipe for twitter", "version": "2.0", "description": "tweets will be translated.", "background_page": "background.html", "content_scripts": [ { "matches": [ "http://twitter.com/*" ], "js": [ "content.js" ] } ] }
contents.js
function convert( str ) { document.getElementById( "status" ).value = str ; document.getElementById("status_update_form").submit( ) ; } function onClick( e ) { e.preventDefault( ) ; e.stopPropagation( ) ; if( document.getElementById( "status" ).value == "" ) return false ; var str = document.getElementById( "status" ).value ; chrome.extension.sendRequest( {'action':'translate', 'str': str }, convert ) ; return ; } document.getElementById("tweeting_button").addEventListener( 'click', onClick, true ) ;
background.html
<!DOCTYPE html> <html> <head> </head> <body> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("language", "1"); function translate( callback, str ) { google.language.translate( str, "ja", "en", function( result ) { callback( result.translation ) ; } ) ; } function onRequest( request, sender, callback ) { if( request.action == 'translate' ) { translate( callback, request.str ) ; } } chrome.extension.onRequest.addListener( onRequest ) ; </script> </body> </html>