pylonshq の文書を翻訳する際に、ページをコピーする権限がない場合は Info > View Source から原文を取得しますが、
ブラウザに表示されるソースをそのままコピーしようとするとインデントが消えてしまったり、
かといって HTML のソースでは HTML の実体参照を置換しなければならなかったりして、余計な手間がかかります。
これは、そんな手間を軽減するための Greasemonkey スクリプトです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 | // ==UserScript==
// @name Pylonsdoc View Source
// @namespace http://wiki.pylonshq.com/display/~knzm/Home
// @include http://wiki.pylonshq.com/display/*
// @include http://wiki.pylonshq.com/pages/viewpage.action*
// ==/UserScript==
(function(){
function getElementsByXPath(xpath, doc, context) {
if (doc == null) doc = document;
if (context == null) context = doc;
var it = doc.evaluate(xpath, context, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var nodes = new Array();
var node;
while ((node = it.iterateNext()) != null) {
nodes.push(node);
}
return nodes;
}
function onViewSourceClicked(ev) {
GM_xmlhttpRequest({
method: 'GET',
url: ev.target.href,
onload: function(req) {
if (req.responseText.match('<div class="padded">[ \n\r]*((?:.|[\r\n])*?)[ \n\r]*</div>')) {
var text = RegExp.$1;
var win = window.open('', null);
win.document.open();
win.document.write('<pre>');
win.document.write(text.replace(/[\r\n]<br>/g, "\n"));
win.document.write('</pre>');
win.document.close();
}
},
});
ev.preventDefault();
return;
}
var info = getElementsByXPath("//a[@accesskey='i']")[0];
if (info && info.href.match(/\?pageId=(\d+)/)) {
var pageId = RegExp.$1;
var a = document.createElement("a");
a.href = 'http://wiki.pylonshq.com/pages/viewpagesrc.action?pageId=' + pageId;
// a.target = '_blank';
a.addEventListener('click', onViewSourceClicked, false);
a.appendChild(document.createTextNode("View source"));
var form = getElementsByXPath("//td[@class='pagecontent']/table//tr[position()=1]//td[position()=1]//form")[0];
if (form != null) {
form.parentNode.insertBefore(a, form);
}
}
})()
|
インストール後、pylonshq の Wiki ページを表示すると、ページの上部に"View source"というリンクが追加されています。
このリンクをクリックすると、別ウィンドウにそのページのソースコードが表示されます。