mirror of
https://github.com/wassname/talk.git
synced 2026-07-14 11:18:50 +08:00
* chore: setup eslint * chore: tslint checks with types & check for import order * chore: complete eslint transition * fix: tests * fix: linting after rebase, faster lint for lint-staged * chore: remove line * fix: lint rules * feat: add a11y linter and fix errors * fix: tests
26 lines
862 B
TypeScript
26 lines
862 B
TypeScript
/**
|
|
* getCurrentScriptOrigin will try to find the script origin.
|
|
* For legacy browsers a fallbackIdentifier is required.
|
|
*
|
|
* @param fallbackID id attached to a script tag to get its origin from for legacy browsrs.
|
|
*/
|
|
function getCurrentScriptOrigin(fallbackID?: string) {
|
|
// Find current script (modern browsers).
|
|
let script = document.currentScript as HTMLScriptElement | null;
|
|
|
|
if (!script && fallbackID) {
|
|
// Find script tag with `fallbackIdentifier` as its id.
|
|
script = document.getElementById(fallbackID) as HTMLScriptElement | null;
|
|
if (!script) {
|
|
// Find script tag with `fallbackIdentifier` as its className.
|
|
script = document.querySelector(`.${fallbackID}`);
|
|
}
|
|
}
|
|
if (!script) {
|
|
throw new Error("Current script not found");
|
|
}
|
|
return new URL(script.src).origin;
|
|
}
|
|
|
|
export default getCurrentScriptOrigin;
|