-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic functionality to alter URL
- Loading branch information
Showing
6 changed files
with
198 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** @license GPL-3.0-or-later | ||
* | ||
* Copyright (C) 2024 8 Hobbies, LLC | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import type { SiteData, SiteDataEntry } from "./site_data"; | ||
|
||
function getMatchedSite(url: string, siteData: SiteData): SiteDataEntry | null { | ||
const matchedSite = siteData.find((siteDataEntry) => | ||
siteDataEntry.urlRegex.exec(url), | ||
); | ||
|
||
return matchedSite ?? null; | ||
} | ||
|
||
/** Returns the updated URL of the functionality of this extension on a given current URL. */ | ||
export function generateActivatingUrl( | ||
url: string, | ||
siteData: SiteData, | ||
): string | null { | ||
const matchedSite = getMatchedSite(url, siteData); | ||
|
||
return matchedSite?.activatingFunc(url) ?? null; | ||
} | ||
|
||
export function generateDisablingUrl( | ||
url: string, | ||
siteData: SiteData, | ||
): string | null { | ||
const matchedSite = getMatchedSite(url, siteData); | ||
|
||
return matchedSite?.disablingFunc(url) ?? null; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** @license GPL-3.0-or-later | ||
* | ||
* Copyright (C) 2024 8 Hobbies, LLC | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { addUrlParam, removeUrlParam } from "./utils"; | ||
|
||
export interface SiteDataEntry { | ||
name: string; | ||
// Regular expression to match the website. | ||
urlRegex: RegExp; | ||
// Function being called when the user visits a website of interest. | ||
activatingFunc: (url: string) => string | null; | ||
// Function being called when the user clicks on the extension icon to disable. | ||
disablingFunc: (url: string) => string | null; | ||
} | ||
|
||
export type SiteData = SiteDataEntry[]; | ||
|
||
export const amazonComParam = { | ||
key: "rh", | ||
value: "n:16310101,p_6:ATVPDKIKX0DER", | ||
}; | ||
|
||
export const builtinSiteData = [ | ||
{ | ||
name: "Amazon.com", | ||
urlRegex: /https:\/\/www\.amazon\.com\/s.*/, | ||
disablingFunc: (url: string): string | null => | ||
removeUrlParam(url, amazonComParam.key), | ||
activatingFunc: (url: string): string | null => | ||
addUrlParam(url, amazonComParam.key, amazonComParam.value), | ||
}, | ||
] as const satisfies SiteData; |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** @license GPL-3.0-or-later | ||
* | ||
* Copyright (C) 2024 8 Hobbies, LLC | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** Add a param to a URL. */ | ||
export function addUrlParam( | ||
url: string, | ||
paramKey: string, | ||
paramValue: string, | ||
): string | null { | ||
const parsedUrl = URL.parse(url); | ||
|
||
// TODO: Impossible to create a case to trigger this part for now. Remove the coverage exception once we can. | ||
/* c8 ignore start */ | ||
if (parsedUrl === null) { | ||
// Not a URL. | ||
console.error(`${url} is not a URL.`); | ||
return null; | ||
} | ||
/* c8 ignore stop */ | ||
|
||
parsedUrl.searchParams.set(paramKey, paramValue); | ||
return parsedUrl.toString(); | ||
} | ||
|
||
/** Remove a param from a URL. */ | ||
export function removeUrlParam(url: string, paramKey: string): string | null { | ||
const parsedUrl = URL.parse(url); | ||
|
||
// TODO: Impossible to create a case to trigger this part for now. Remove the coverage exception once we can. | ||
/* c8 ignore start */ | ||
if (parsedUrl === null) { | ||
// Not a URL. | ||
console.error(`${url} is not a URL.`); | ||
return null; | ||
} | ||
/* c8 ignore stop */ | ||
|
||
parsedUrl.searchParams.delete(paramKey); | ||
return parsedUrl.toString(); | ||
} |