-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nevo David
committed
Sep 23, 2024
1 parent
25aa3d5
commit ec0259d
Showing
5 changed files
with
94 additions
and
2,923 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,6 @@ | ||
import {allTwoLevelSubdomain} from "./all.two.level.subdomain"; | ||
const ipRegex = /^(https?:\/\/)?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+)?$/; | ||
|
||
export function removeSubdomain(domain: string) { | ||
// Check if the domain is an IP address with optional port | ||
if (ipRegex.test(domain)) { | ||
return domain; // Return the original domain if it's an IP address | ||
} | ||
// Split the domain into its parts | ||
const parts = domain.split('.'); | ||
|
||
// Check if there are at least two parts (e.g., 'example.com') | ||
if (parts.length < 2) { | ||
return domain; // Return the original domain if it's too short to have a subdomain | ||
} | ||
|
||
if (parts.length > 2) { | ||
const lastTwo = parts.slice(-2).join('.'); | ||
if (allTwoLevelSubdomain.includes(lastTwo)) { | ||
return 'https://' + parts.slice(-3).join('.'); // Return the last three parts for known second-level domains | ||
} | ||
} | ||
|
||
// Return the last two parts for standard domains | ||
return 'https://' + parts.slice(-2).join('.'); | ||
} | ||
|
||
import { parse } from 'tldts'; | ||
|
||
export function getCookieUrlFromDomain(domain: string) { | ||
const url = removeSubdomain(domain); | ||
const urlObj = new URL(url); | ||
if (!ipRegex.test(domain)) { | ||
return '.' + urlObj.hostname | ||
} | ||
|
||
return urlObj.hostname; | ||
const url = parse(domain); | ||
return url.domain! ? "." + url.domain! : url.hostname!; | ||
} |
Oops, something went wrong.