Firefox Quantumとgreasemonkeyとlibron - 隠居日録の続き。或る日気づいたら、図書館の変更の所をクリックしても、いつまでたってもリストが表示されなくなっていた。
いつまでたってもデータ取得中が表示されたままになっている。少なくともあの記事を書いたときはできたはずだ。図書館を変更することはあまりないので問題がないのだが、なんとなく気になるので調べてみると、updateLibrarySelectBoxでのリストの更新が完了していないことが問題の様だった。更に詳しく調べてみると、GM.xmlHttpRequestによるhttp://calil.jp/city_listの取得に問題があり、なぜかすべての結果がこの関数で参照できずに、後半の所がとりきれていないのだ。そのため、正規表現でのマッチングに失敗し、JSONの処理で失敗するので、それ以降の処理が行われないことが分かった。
なぜ、すべてのデータが読み込めなくなったのかは不明だ。Firefoxの更新なのか、greasemonekyの更新なのか、calil.jpからのデータが増えたのか。
しかたがないので、とりあえず次のように処理できそうなところまで処理するように変更することで回避した。ただし、鹿児島県の途中の所までしか取得できていないので、鹿児島県の一部と沖縄県の図書館のリストは表示されない。
function updateLibrarySelectBox(selectBoxDiv, prefecture, univ) {
if (!univ) univ = false;
if (libron.libraryNames[prefecture]) {
selectBoxDiv.replaceChild(createLibrarySelectBox(libron.libraryNames[prefecture], univ), selectBoxDiv.childNodes[3]);
} else {
var url = "http://api.calil.jp/library?appkey=" + encodeURIComponent(libron.appkey) + "&pref=" + encodeURIComponent(prefecture) + "&format=json";
if (isSafariExtension()) {
safari.self.tab.dispatchMessage("retrieveLibraryInfo", [url, prefecture, univ]);
} else {
GM.xmlHttpRequest({
method: "GET",
url: url,
onload: function(response){
GM.xmlHttpRequest({
method: "GET",
url: "http://calil.jp/city_list",
onload: function(city_list_response) {
var city_list_match = city_list_response.responseText.match(/^loadcity\((.*)\);$/);
if (city_list_match == null) {
city_list_match = city_list_response.responseText.match(/^loadcity\((.*)},/);
city_list_match[1] = city_list_match[1] + "}}";
}
var cities = JSON.parse(city_list_match[1]);
var match = response.responseText.match(/^callback\((.*)\);$/);
libron.libraries[prefecture] = JSON.parse(match[1]);
libron.libraryNames[prefecture] = createLibraryNames(prefecture, libron.libraries, cities[prefecture]);
selectBoxDiv.replaceChild(createLibrarySelectBox(libron.libraryNames[prefecture], univ), selectBoxDiv.childNodes[3]);
}
});
}
});
}
}
}
2018年1月12日追記
公式から3.0.10がリリースされて、この更新中のままになる問題は修正されたようだ。変更点はJSONデータを探す時の正規表現が変わったのと、calil.jpへのアクセスがSSLになったことのようだが、後者の方が効いているということなのだろうか?
function updateLibrarySelectBox(selectBoxDiv, prefecture, univ) {
if (!univ) univ = false;
if (libron.libraryNames[prefecture]) {
selectBoxDiv.replaceChild(createLibrarySelectBox(libron.libraryNames[prefecture], univ), selectBoxDiv.childNodes[3]);
} else {
var url = "https://api.calil.jp/library?appkey=" + encodeURIComponent(libron.appkey) + "&pref=" + encodeURIComponent(prefecture) + "&format=json";
if (isSafariExtension()) {
safari.self.tab.dispatchMessage("retrieveLibraryInfo", [url, prefecture, univ]);
} else {
GM.xmlHttpRequest({
method: "GET",
url: url,
onload: function(response){
GM.xmlHttpRequest({
method: "GET",
url: "https://calil.jp/city_list",
onload: function(city_list_response) {
var city_list_match = city_list_response.responseText.match(/^loadcity\((.*)\);(\n)*$/);
var cities = JSON.parse(city_list_match[1]);
var match = response.responseText.match(/^callback\((.*)\);(\n)*$/);
libron.libraries[prefecture] = JSON.parse(match[1]);
libron.libraryNames[prefecture] = createLibraryNames(prefecture, libron.libraries, cities[prefecture]);
selectBoxDiv.replaceChild(createLibrarySelectBox(libron.libraryNames[prefecture], univ), selectBoxDiv.childNodes[3]);
}
});
}
});
}
}
}
ただし、現在のfirefox版のlibronはgreasemonkeyではなくTampermonkeyを使うよう勧めている。