Skip to content

Commit f2a4162

Browse files
committed
kunai.js: crsearch.jsonの位置を自動で特定
1 parent 4cfc0eb commit f2a4162

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

js/kunai.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,58 @@ class Kunai {
116116

117117
await this.initSidebar()
118118

119+
// Dynamically set the base_url
120+
const dynamic_base_url = (() => {
121+
// Determine the location of the website base
122+
const current_script = document.currentScript || document.querySelector('script[src*="static/kunai/js/kunai.js"]')
123+
if (current_script) {
124+
// Try to determine the base_url based on the location of this script
125+
// ({base_url}/static/kunai/js/kunai.js).
126+
const url_kunai = current_script.getAttribute("src")
127+
const url = url_kunai.replace(/\bstatic\/kunai\/js\/kunai\.js([?#].*)?$/, "")
128+
if (url != url_kunai) return url == "" ? "/" : url
129+
}
130+
// Fallback case assuming that the website is hosted at the top level
131+
return "/"
132+
})()
133+
134+
const database_url = (() => {
135+
// Determine the location of the database file "crsearch.json".
136+
const current_script = document.currentScript || document.querySelector('script[src*="kunai/js/kunai.js"]')
137+
if (current_script) {
138+
// Try to download crsearch.json from the project website for local
139+
// HTML files. When a HTML in a local file system is directly opened
140+
// in a Web browser, "static/crsearch/crsearch.json" cannot be read
141+
// through XHR due to the CORS (cross-origin resource sharing) policy
142+
// for the local files. We instead try to download "crsearch.json"
143+
// from the project website, which is assumed to be stored in <meta
144+
// name="twietter:url" content="..." /> or in <meta property="og:url"
145+
// content="..." />.
146+
if (/^file:\/\//.test(current_script.src)) {
147+
const meta = document.querySelector('meta[name="twitter:url"]') || document.querySelector('meta[property="og:url"]')
148+
if (meta && meta.content) {
149+
const m = meta.content.toString().match(/^https?:\/\/[^/]*\//)
150+
if (m) return m[0] + "static/crsearch/crsearch.json"
151+
}
152+
}
153+
154+
// Try to determine the position of crsearch.json
155+
// ({base_url}/static/crsearch/crsearch.json) based on the location of
156+
// this script ({base_url}/static/kunai/js/kunai.js).
157+
const url_kunai = current_script.getAttribute("src")
158+
const url = url_kunai.replace(/\bkunai\/js\/kunai\.js([?#].*)?$/, "crsearch/crsearch.json")
159+
if (url != url_kunai) return url
160+
}
161+
162+
// Fallback case assuming that the website is hosted at the top level
163+
return "/static/crsearch/crsearch.json"
164+
})();
165+
119166
let crs = new CRSearch({
120167
onDatabase: this.onDatabase.bind(this),
168+
base_url: dynamic_base_url
121169
})
122-
crs.database('/static/crsearch/crsearch.json')
170+
crs.database(database_url)
123171

124172
let e = $('.crsearch')
125173
await crs.searchbox(e)

0 commit comments

Comments
 (0)