1 | (function() {
|
---|
2 | function KeywordMatch(parent, child, before, match, after) {
|
---|
3 | this.parent = parent;
|
---|
4 | this.child = child;
|
---|
5 | this.before = before;
|
---|
6 | this.match = match;
|
---|
7 | this.after = after;
|
---|
8 | }
|
---|
9 |
|
---|
10 | function findKeywordFromNode(keyword, node) {
|
---|
11 | var matches = [];
|
---|
12 | for (var i = 0; i < node.childNodes.length; i++) {
|
---|
13 | var childNode = node.childNodes[i];
|
---|
14 | if (childNode.nodeType == Node.TEXT_NODE) {
|
---|
15 | var keywordPattern = new RegExp('^((.|\\n)*)(' + keyword.replace('.', '\\.')
|
---|
16 | + ')((.|\\n)*)$', 'm');
|
---|
17 | if (childNode.nodeValue.match(keywordPattern)) {
|
---|
18 | matches.push(new KeywordMatch(node, childNode, RegExp.$1, RegExp.$3, RegExp.$4));
|
---|
19 | }
|
---|
20 | }
|
---|
21 | else {
|
---|
22 | findKeywordFromNode(keyword, node.childNodes[i]);
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | for (var i = 0; i < matches.length; i++) {
|
---|
27 | var match = matches[i];
|
---|
28 | var name = document.createElement('a');
|
---|
29 | name.id = keyword;
|
---|
30 | name.name = keyword;
|
---|
31 | var coloredMatch = document.createElement('span');
|
---|
32 | coloredMatch.className = 'match';
|
---|
33 | coloredMatch.appendChild(document.createTextNode(match.match));
|
---|
34 | match.parent.insertBefore(document.createTextNode(match.before), match.child);
|
---|
35 | match.parent.insertBefore(name, match.child);
|
---|
36 | match.parent.insertBefore(coloredMatch, match.child);
|
---|
37 | match.parent.insertBefore(document.createTextNode(match.after), match.child);
|
---|
38 | match.parent.removeChild(match.child);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | function findKeyword() {
|
---|
43 | var keywordField = document.createElement('input');
|
---|
44 | keywordField.id = 'keyword';
|
---|
45 | keywordField.onfocus = function() {
|
---|
46 | setTimeout(function() {keywordField.select()}, 10);
|
---|
47 | };
|
---|
48 | var keyword = document.location.hash;
|
---|
49 | if (keyword) {
|
---|
50 | keyword = keyword.substring(1);
|
---|
51 | keywordField.value = keyword;
|
---|
52 | findKeywordFromNode(keyword, document.body);
|
---|
53 | }
|
---|
54 |
|
---|
55 | var keywordButton = document.createElement('button');
|
---|
56 | keywordButton.innerHTML = 'Search';
|
---|
57 | keywordButton.onclick = function() {
|
---|
58 | document.location.hash = document.getElementById('keyword').value;
|
---|
59 | document.location.reload();
|
---|
60 | };
|
---|
61 | keywordField.onkeyup = function(evt) {
|
---|
62 | if (evt.keyCode == 13) keywordButton.onclick();
|
---|
63 | };
|
---|
64 |
|
---|
65 | var keywordBox = document.createElement('div');
|
---|
66 | keywordBox.className = 'keyword-box';
|
---|
67 | keywordBox.appendChild(keywordField);
|
---|
68 | keywordBox.appendChild(keywordButton);
|
---|
69 | document.body.appendChild(keywordBox);
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (window.addEventListener) {
|
---|
73 | //window.addEventListener('DOMContentLoaded', findKeyword, false);
|
---|
74 | window.addEventListener('load', findKeyword, false);
|
---|
75 | }
|
---|
76 | else if (window.attachEvent) {
|
---|
77 | window.attachEvent('onload', findKeyword);
|
---|
78 | }
|
---|
79 | })();
|
---|