Skip to content

Instantly share code, notes, and snippets.

@kinngh
Created November 6, 2024 11:13
Show Gist options
  • Save kinngh/380c090e07f09fdad4a67dc0d782653e to your computer and use it in GitHub Desktop.
Save kinngh/380c090e07f09fdad4a67dc0d782653e to your computer and use it in GitHub Desktop.
Pull passwordless demo store URL
async function getHrefFromUrl(url) {
try {
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
Connection: "keep-alive",
},
});
const html = await response.text();
const pattern = /<a[^>]*href="([^"]*)"[^>]*>\s*View demo store\s*<\/a>/;
const match = html.match(pattern);
const href = match ? match[1] : null;
console.log(href);
return href;
} catch (error) {
console.error("Error:", error);
}
}
// Usage
const url = "https://apps.shopify.com/{app-handle}";
await getHrefFromUrl(url);
@kinngh
Copy link
Author

kinngh commented Nov 6, 2024

Why?

Demo stores, when not accessed from the App Store usually present a password. It is possible to pull the passwordless link from the App Store and redirect the user. I also don't want to use unnecessary dependencies so simple regex is doing the job.

Usecase

URL shortener. Use this as a redirector where https://your-app-url.com/demo calls the function and redirects the user to the demo store, eliminating the need for a password. Also works really great if you are on a call and want to share a straight forward link with a demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment