summaryrefslogtreecommitdiff
path: root/assets/js/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/js/main.js')
-rw-r--r--assets/js/main.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/assets/js/main.js b/assets/js/main.js
index e926618..ed21874 100644
--- a/assets/js/main.js
+++ b/assets/js/main.js
@@ -17,6 +17,25 @@ function initVisitors() {
}
}
+function highlightKeyword() {
+ var match = location.search.match(/[?&]kw=([^&]+)/);
+ var kw = match ? $.trim(decodeURIComponent(match[1].replace(/\+/g, ' '))) : '';
+ if (!kw) return;
+
+ var reg = new RegExp('(' + kw.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'gi');
+ var escapeMap = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
+
+ $('section, section *').not('script, style, textarea').contents().filter(function () {
+ return this.nodeType === 3;
+ }).each(function () {
+ var escapedText = this.nodeValue.replace(/[&<>"']/g, function (m) { return escapeMap[m]; });
+ var highlighted = escapedText.replace(reg, '<mark>$1</mark>');
+ if (escapedText !== highlighted) {
+ $(this).replaceWith(highlighted);
+ }
+ });
+}
+
$(function () {
(function () {
var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
@@ -42,6 +61,7 @@ $(function () {
});
console.warn("Mayx may already be Dead");
}
+ highlightKeyword();
});
function getSearchJSON(callback) {