Page 1 of 1

Gibt es eine Möglichkeit, alle Weiterleitungsorte von einem bestimmten XML/HTML -Skript in Google Apps zu erhalten?

Posted: 20 Aug 2025, 00:34
by Anonymous
Ich habe ein Google -Blatt, in dem eine Apps -Skriptfunktion mit dem Namen get_redirect_url () vorhanden ist und nicht ordnungsgemäß funktioniert. Gibt es eine Möglichkeit, dies zu beheben? In der VSCODE -Blog -URL gibt es beispielsweise eine Meta -Aktualisierungsumleitung, und die Funktion kann sie nicht extrahieren, wodurch dieser Fehler wirft:

Fehler: ReferenzError: URL ist nicht definiert. Auch einige andere URLs werfen entweder einen anderen Fehler oder fangen nicht ab.


Visual Studio Code Blogs






< /code>
Dies ist meine Apps -Skript -Implementierung: < /p>
/**
* Returns the redirect location of a url.
*
* @param {string} input The source URL being redirected.
* @return The destination location/URL.
* @customfunction
*/
function GET_REDIRECT_LOCATION(input) {
// Basic validation
if (!input) return "INVALID_URL";

let url = input.toString().trim();
if (!url || !url.includes(".")) return "INVALID_URL";

// Add https if no protocol
if (!url.startsWith("http")) {
url = "https://" + url;
}

try {
// First try with followRedirects: false to catch HTTP redirects
let response = UrlFetchApp.fetch(url, {
muteHttpExceptions: true,
followRedirects: false
});

let status = response.getResponseCode();

// Check for HTTP redirect (3xx status codes)
if (status >= 300 && status < 400) {
const headers = response.getAllHeaders();
const location = headers.Location || headers.location;

if (location) {
return location;
}
}

// Get the HTML content to check for meta redirects
const content = response.getContentText();

// Check for meta refresh redirect
const metaRefresh = content.match(/content=["']?\d+;\s*url=([^"'\s>]+)["']?/i);
if (metaRefresh && metaRefresh[1]) {
let redirectUrl = metaRefresh[1];

// Handle relative URLs - make them absolute
if (redirectUrl.startsWith('/')) {
const baseUrl = new URL(url);
redirectUrl = baseUrl.protocol + '//' + baseUrl.host + redirectUrl;
}

return redirectUrl;
}

// Check for canonical URL as backup
const canonical = content.match(/]*rel=["']canonical["'][^>]*href=["']([^"']+)["']/i);
if (canonical && canonical[1]) {
let canonicalUrl = canonical[1];

// Handle relative URLs
if (canonicalUrl.startsWith('/')) {
const baseUrl = new URL(url);
canonicalUrl = baseUrl.protocol + '//' + baseUrl.host + canonicalUrl;
}

if (canonicalUrl !== url) {
return canonicalUrl;
}
}

return "NO_REDIRECTS_FOUND";

} catch (error) {
return "ERROR: " + error.toString();
}
}
< /code>
Auch nach dem Versuch, diesen Kantenfall zu handhaben, funktioniert es immer noch nicht. Hat jemand irgendwelche Lösungen?>