|
"use strict"; |
|
var INFO = [ |
|
"plugin", |
|
{ |
|
name: "kuroshiro", |
|
version: "1.1", |
|
href: "http://genjit.su", |
|
summary: "Add furigana to Japanese text", |
|
xmlns: "dactyl" |
|
}, |
|
[ |
|
"author", |
|
{ |
|
email: "[email protected]" |
|
}, |
|
"Akasaka Ryuunosuke" |
|
], |
|
[ |
|
"project", |
|
{ |
|
name: "Pentadactyl", |
|
"min-version": "1.0" |
|
} |
|
], |
|
[ |
|
"p", {}, |
|
"Adds furigana to Japanese text" |
|
], |
|
]; |
|
|
|
function info_add_description(tags, description) { |
|
INFO = INFO.concat( |
|
[ |
|
"item", {}, |
|
["tags", {}, tags], |
|
["spec", {}, tags], |
|
["description", {}, |
|
["p", {}, description] |
|
] |
|
] |
|
); |
|
} |
|
|
|
|
|
function textNodesUnder(el) { |
|
var n, a = [], |
|
walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false); |
|
while (n = walk.nextNode()) a.push(n); |
|
var b = []; |
|
for (var i in a) { |
|
if (b.indexOf(a[i].parentNode) < 0 && a[i].textContent && a[i].textContent.trim().length > 0) |
|
b.push(a[i].parentNode); |
|
} |
|
return b; |
|
} |
|
|
|
var kspool = 0; |
|
|
|
function do_ksh() { |
|
dactyl.echo("performing kuroshiro..."); |
|
let nodes = textNodesUnder(gBrowser.mCurrentTab.linkedBrowser.contentDocument.body); |
|
let errors = 0; |
|
for (var i in nodes) { |
|
let node = nodes[i]; |
|
let http = new XMLHttpRequest(); |
|
var url = 'http://127.0.0.1:9615/'; |
|
if ('undefined' == typeof node) continue; |
|
var prm = { |
|
text: node.innerHTML |
|
}; |
|
var params = JSON.stringify(prm); |
|
http.open('POST', url, true); |
|
|
|
//Send the proper header information along with the request |
|
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
|
|
|
http.onreadystatechange = () => { //Call a function when the state changes. |
|
if (http.readyState == 4 && http.status == 200) { |
|
let response = JSON.parse(http.responseText); |
|
if (response.need) { |
|
node.innerHTML = response.new; |
|
} |
|
kspool--; |
|
if (kspool == 0) { |
|
dactyl.echo("kuroshiro: done"); |
|
} else { |
|
dactyl.echo("kuroshiro: response [" + kspool + "]"); |
|
} |
|
} |
|
}; |
|
kspool++; |
|
http.send(params); |
|
} |
|
|
|
dactyl.echo("kuroshiro: request sent"); |
|
} |
|
|
|
function create_command_and_mapping(command, description, funcref, mapping, command_option = {}) { |
|
group.commands.add([command], description, funcref, command_option, true); |
|
if (mapping != "") |
|
group.mappings.add([modes.NORMAL], [mapping], description, funcref); |
|
info_add_description(":" + command + " " + mapping, description); |
|
} |
|
|
|
create_command_and_mapping("kuroshiro", "Add furigana to page", |
|
function() { |
|
do_ksh(); |
|
}, ""); |