Add Rhymix.getBaseUrl() and remove internal cache variable "baseurl"

This commit is contained in:
Kijin Sung 2025-05-25 16:29:39 +09:00
parent 341df211ee
commit 1568cb3790

View file

@ -9,7 +9,6 @@
*/ */
const Rhymix = { const Rhymix = {
baseurl: null,
addedDocument: [], addedDocument: [],
langCodes: {}, langCodes: {},
loadedPopupMenus: [], loadedPopupMenus: [],
@ -18,7 +17,8 @@ const Rhymix = {
pendingDebugData: [], pendingDebugData: [],
showAjaxErrors: ['ALL'], showAjaxErrors: ['ALL'],
unloading: false, unloading: false,
modal: {} modal: {},
state: {}
}; };
/** /**
@ -106,7 +106,7 @@ Rhymix.getLangType = function() {
* @return void * @return void
*/ */
Rhymix.setLangType = function(lang_type) { Rhymix.setLangType = function(lang_type) {
const baseurl = this.URI(default_url).pathname(); const baseurl = this.getBaseUrl();
if (baseurl !== '/') { if (baseurl !== '/') {
this.cookie.remove('lang_type', { path: '/' }); this.cookie.remove('lang_type', { path: '/' });
} }
@ -142,7 +142,19 @@ Rhymix.getRewriteLevel = function() {
}; };
/** /**
* Get the default URL * Get the base URL relative to the current origin
*
* @return string
*/
Rhymix.getBaseUrl = function() {
if (!this.state.baseUrl) {
this.state.baseUrl = this.URI(default_url).pathname();
}
return this.state.baseUrl;
};
/**
* Get the full default URL
* *
* @return string * @return string
*/ */
@ -227,20 +239,20 @@ Rhymix.isSameHost = function(url) {
if (url.match(/^\w+:[^\/]*$/) || url.match(/^(https?:)?\/\/[^\/]*[^a-z0-9\/.:_-]/i)) { if (url.match(/^\w+:[^\/]*$/) || url.match(/^(https?:)?\/\/[^\/]*[^a-z0-9\/.:_-]/i)) {
return false; return false;
} }
if (!this.baseurl) { if (!this.state.partialOrigin) {
this.baseurl = this.URI(window.request_uri).normalizePort().normalizeHostname().normalizePathname(); let uri = this.URI(window.request_uri).normalizePort().normalizeHostname().normalizePathname();
this.baseurl = this.baseurl.hostname() + this.baseurl.directory(); this.state.partialOrigin = uri.hostname() + uri.directory();
} }
try { try {
let target_url = this.URI(url).normalizePort().normalizeHostname().normalizePathname(); let target_url = this.URI(url).normalizePort().normalizeHostname().normalizePathname();
if (target_url.is("urn")) { if (target_url.is('urn')) {
return false; return false;
} }
if (!target_url.hostname()) { if (!target_url.hostname()) {
target_url = target_url.absoluteTo(window.request_uri); target_url = target_url.absoluteTo(window.request_uri);
} }
target_url = target_url.hostname() + target_url.directory(); target_url = target_url.hostname() + target_url.directory();
return target_url.indexOf(this.baseurl) === 0; return target_url.indexOf(this.state.partialOrigin) === 0;
} catch(err) { } catch(err) {
return false; return false;
} }