| 1 |
window.onload = function () { |
| 2 |
createToolbar (); |
| 3 |
initializeHatenaStar(); |
| 4 |
enableHTML5Support (); |
| 5 |
}; // window.onload |
| 6 |
|
| 7 |
function getElementsByClassName (c) { |
| 8 |
if (document.getElementsByClassName) { |
| 9 |
return document.getElementsByClassName (c); |
| 10 |
} else { |
| 11 |
return []; |
| 12 |
} |
| 13 |
} // getElementsByClassName |
| 14 |
|
| 15 |
function getAncestorElement (n, t) { |
| 16 |
while (n != null) { |
| 17 |
if (n.nodeName == t) { |
| 18 |
return n; |
| 19 |
} else { |
| 20 |
n = n.parentNode; |
| 21 |
} |
| 22 |
} |
| 23 |
return null; |
| 24 |
} // getAncestorElement |
| 25 |
|
| 26 |
function getNextAnchorNumber (s) { |
| 27 |
var lastId = 0; |
| 28 |
s.replace (/\[([0-9]+)\]/g, function (l, n) { |
| 29 |
var v = parseInt (n); |
| 30 |
if (v > lastId) { |
| 31 |
lastId = v; |
| 32 |
} |
| 33 |
}); |
| 34 |
return lastId + 1; |
| 35 |
} // getNextAnchorNumber |
| 36 |
|
| 37 |
function createToolbar () { |
| 38 |
var containers = getElementsByClassName ('text-toolbar'); |
| 39 |
for (var i = 0; i < containers.length; i++) { |
| 40 |
var container = containers[i]; |
| 41 |
|
| 42 |
var button = document.createElement ('button'); |
| 43 |
button.setAttribute ('type', 'button'); |
| 44 |
button.innerHTML = '[#]'; |
| 45 |
button.onclick = function () { |
| 46 |
var form = getAncestorElement (container, 'FORM'); |
| 47 |
var ta = form.elements.text; |
| 48 |
var st = ta.scrollTop; |
| 49 |
var ss = ta.selectionStart; |
| 50 |
var se = (ss != ta.selectionEnd); |
| 51 |
var added = '[' + getNextAnchorNumber (ta.value) + '] '; |
| 52 |
/* |
| 53 |
if (ss > 0 && ta.value.substring (ss - 1, ss) != "\n") { |
| 54 |
added = "\n" + added; |
| 55 |
} |
| 56 |
*/ |
| 57 |
ta.value = ta.value.substring (0, ta.selectionStart) |
| 58 |
+ added + ta.value.substring (ta.selectionEnd); |
| 59 |
if (se) { |
| 60 |
ta.setSelectionRange (ss, ss + added.length); |
| 61 |
} else { |
| 62 |
ta.setSelectionRange (ss + added.length, ss + added.length); |
| 63 |
} |
| 64 |
ta.scrollTop = st; |
| 65 |
ta.focus (); |
| 66 |
}; // button.onclick |
| 67 |
container.appendChild (button); |
| 68 |
} |
| 69 |
} // createToolbar |
| 70 |
|
| 71 |
function initializeHatenaStar () { |
| 72 |
if (!window.Hatena) { |
| 73 |
window.Hatena = {}; |
| 74 |
} |
| 75 |
if (!Hatena.Star) { |
| 76 |
Hatena.Star = {}; |
| 77 |
} |
| 78 |
if (!Hatena.Star.onLoadFunctions) { |
| 79 |
Hatena.Star.onLoadFunctions = []; |
| 80 |
} |
| 81 |
|
| 82 |
Hatena.Star.onLoadFunctions.push (function () { |
| 83 |
/* |
| 84 |
if (Ten.DOM.loaded) { |
| 85 |
Ten.DOM.loaded = false; |
| 86 |
Ten.DOM.addObserver (); |
| 87 |
} |
| 88 |
*/ |
| 89 |
|
| 90 |
Hatena.Star.SiteConfig = { |
| 91 |
entryNodes: { |
| 92 |
'' /* body */: { |
| 93 |
uri: 'h1 a', |
| 94 |
title: 'h1', |
| 95 |
container: '.tools' /* .nav.tools */ |
| 96 |
} |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
/* |
| 101 |
var realLoadNewEntries = Hatena.Star.EntryLoader.loadNewEntries; |
| 102 |
Hatena.Star.EntryLoader.loadNewEntries = function (node) { |
| 103 |
if (!node) { |
| 104 |
node = document.documentElement; |
| 105 |
} |
| 106 |
realLoadNewEntries.apply (this, [node]); |
| 107 |
}; |
| 108 |
*/ |
| 109 |
setTimeout (function () { |
| 110 |
Ten.DOM.dispatchEvent ('onload'); |
| 111 |
}, 1); |
| 112 |
}); // Hatena.Star.onLoadFunctions.push'ed function |
| 113 |
|
| 114 |
var hsScript = document.createElement ('script'); |
| 115 |
hsScript.defer = true; |
| 116 |
hsScript.src = 'http://s.hatena.ne.jp/js/HatenaStar.js'; |
| 117 |
document.documentElement.lastChild.appendChild (hsScript); |
| 118 |
|
| 119 |
/* |
| 120 |
In SuikaWiki, a WikiName can be associated with multiple WikiPages, |
| 121 |
while a WikiPage can be associated with multiple WikiNames. It would |
| 122 |
be desired to associate Hatena Stars with WikiPages in theory. |
| 123 |
However, WikiPages do not have permalinks at the moment. |
| 124 |
|
| 125 |
See <http://suika.fam.cx/%7Ewakaba/wiki/sw/n/%E3%81%AF%E3%81%A6%E3%81%AA%E3%82%B9%E3%82%BF%E3%83%BC#anchor-2>. |
| 126 |
*/ |
| 127 |
} // initializeHatenaStar |
| 128 |
|
| 129 |
function enableHTML5Support () { |
| 130 |
window.TEROnLoad = function () { |
| 131 |
new TER.Delta (document.body); |
| 132 |
}; // window.TEROnLoad |
| 133 |
|
| 134 |
var timeScript = document.createElement ('script'); |
| 135 |
timeScript.defer = true; |
| 136 |
timeScript.charset = 'utf-8'; |
| 137 |
timeScript.src = 'http://suika.fam.cx/www/style/ui/time.js'; |
| 138 |
document.documentElement.lastChild.appendChild (timeScript); |
| 139 |
} // enableHTML5Support |
| 140 |
|
| 141 |
/* Hack for IE */ |
| 142 |
document.createElement ('time'); |