63 lines
1.3 KiB
JavaScript
63 lines
1.3 KiB
JavaScript
function findTokenInText(text) {
|
|
if (!text) {
|
|
return "";
|
|
}
|
|
|
|
const queryIndex = text.indexOf("?");
|
|
if (queryIndex === -1) {
|
|
return "";
|
|
}
|
|
|
|
const rawQueryText = text.slice(queryIndex + 1);
|
|
const hashIndex = rawQueryText.indexOf("#");
|
|
const queryText =
|
|
hashIndex === -1 ? rawQueryText : rawQueryText.slice(0, hashIndex);
|
|
const search = new URLSearchParams(queryText);
|
|
return search.get("token") || "";
|
|
}
|
|
|
|
export function extractTokenFromUrl(url) {
|
|
if (!url) {
|
|
return "";
|
|
}
|
|
|
|
const directToken = findTokenInText(url);
|
|
if (directToken) {
|
|
return directToken;
|
|
}
|
|
|
|
const hashIndex = url.indexOf("#");
|
|
if (hashIndex === -1) {
|
|
return "";
|
|
}
|
|
|
|
return findTokenInText(url.slice(hashIndex + 1));
|
|
}
|
|
|
|
export function getCurrentWebviewUrl() {
|
|
// #ifdef H5
|
|
return window.location.href || "";
|
|
// #endif
|
|
|
|
// #ifdef APP-PLUS
|
|
if (typeof plus === "undefined" || !plus.webview) {
|
|
return "";
|
|
}
|
|
|
|
const currentWebview =
|
|
plus.webview.currentWebview() || plus.webview.getLaunchWebview();
|
|
|
|
if (!currentWebview || typeof currentWebview.getURL !== "function") {
|
|
return "";
|
|
}
|
|
|
|
return currentWebview.getURL() || "";
|
|
// #endif
|
|
|
|
return "";
|
|
}
|
|
|
|
export function getCurrentWebviewToken() {
|
|
return extractTokenFromUrl(getCurrentWebviewUrl());
|
|
}
|