Skip to content

Instantly share code, notes, and snippets.

@taiju
Created December 6, 2010 09:54
Show Gist options
  • Save taiju/730079 to your computer and use it in GitHub Desktop.
Save taiju/730079 to your computer and use it in GitHub Desktop.
iPad用のソース表示ブックマークレット
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>view-source</title>
</head>
<body>
<p>以下のリンク先をBookmarkに追加する。</p>
<a href="javascript:(function(){var script=document.createElement('script');script.type='text/javascript';script.src='/path/to/view_source.js';document.body.appendChild(script);})()">ソースを表示</a>
</body>
</html>
(function() {
/* Google Code Prettifyを利用
下記URLよりダウンロード
- http://code.google.com/p/google-code-prettify/
変数:
csspath: 公開サーバーにprettify.cssを設置し、そのURLを設定
jspath: 公開サーバーにprettify.jsを設置し、そのURLを設定
linenumber: 行番号が必要な場合はtrue、行番号が不要な場合はfalse */
var csspath = '/path/to/prettify.css';
var jspath = '/path/to/prettify.js';
var linenumber = true;
var req = new XMLHttpRequest();
req.open('GET', location.href, true);
req.overrideMimeType('text/html;charset=' + document.characterSet);
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
var head = document.getElementsByTagName('head')[0];
var meta = Array.prototype.slice.call(document.getElementsByTagName('meta'));
var body = document.getElementsByTagName('body')[0];
var title = document.getElementsByTagName('title')[0];
var link = document.createElement('link');
link.setAttribute('href', csspath);
link.setAttribute('type', 'text/css');
link.setAttribute('rel', 'stylesheet');
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', jspath);
var pre = document.createElement('pre');
pre.textContent = req.responseText;
pre.setAttribute('class', 'prettyprint' + (linenumber ? ' linenums' : ''));
pre.style.margin = '0';
pre.style.padding = '0';
pre.style.border = '0';
head.textContent = '';
Array.prototype.forEach.call(meta, function(el) {
head.appendChild(el);
});
head.appendChild(title);
head.appendChild(link);
head.appendChild(script);
body.textContent = '';
body.appendChild(pre);
body.style.margin = '0';
body.style.padding = '10px';
(function() {
var func = arguments.callee;
if (typeof prettyPrint === 'function') {
prettyPrint();
}
else {
setTimeout(func, 300);
}
return;
})();
}
}
}
req.send(null);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment