From 392f5b8fc62bac5a03fed800ade91937ee66d49b Mon Sep 17 00:00:00 2001
From: Mayank Rana
Date: Fri, 23 Feb 2024 20:55:37 +0530
Subject: [PATCH 1/3] fix: update UI and logic to process ccTLD
---
.../extension/src/utils/findRWSURLSets.ts | 71 ++++++++++++++++---
.../relatedWebsiteSets/insights/index.tsx | 34 +++++----
.../insights/utils/checkURLInRWS.ts | 21 ++++--
3 files changed, 99 insertions(+), 27 deletions(-)
diff --git a/packages/extension/src/utils/findRWSURLSets.ts b/packages/extension/src/utils/findRWSURLSets.ts
index d24dd8456..d02f87ffe 100644
--- a/packages/extension/src/utils/findRWSURLSets.ts
+++ b/packages/extension/src/utils/findRWSURLSets.ts
@@ -23,21 +23,72 @@ import { getDomain } from 'tldts';
*/
import type { RelatedWebsiteSetType } from '../@types';
+export type RWSSetOutputType = RelatedWebsiteSetType & {
+ ccTLDParent?: string;
+};
+
const findRWSURLSets = (
domain: string | null,
rwsSets: RelatedWebsiteSetType[]
-): RelatedWebsiteSetType | undefined => {
- return rwsSets.find((rws: RelatedWebsiteSetType) => {
- const rwsDomains: string[] = Object.keys(rws.rationaleBySite || {}).map(
- (_url) => getDomain(_url) || ''
- );
-
- if (domain === getDomain(rws.primary)) {
- return true;
+): RWSSetOutputType | undefined => {
+ if (!domain) {
+ return undefined;
+ }
+
+ let ccTLDParentURL = '';
+
+ const res: RWSSetOutputType | undefined = rwsSets.find(
+ (rws: RelatedWebsiteSetType) => {
+ if (domain === getDomain(rws.primary)) {
+ return true;
+ }
+
+ const rwsDomains: string[] = Object.keys(rws.rationaleBySite || {}).map(
+ (_url) => getDomain(_url) || ''
+ );
+
+ if (rwsDomains.includes(domain)) {
+ return true;
+ }
+
+ const ccTLDs = Object.entries(rws.ccTLDs || {}).reduce(
+ (acc, [ccTLDParent, _ccTLDs]: [string, string[]]) => {
+ const ccTLDsObj = _ccTLDs.reduce(
+ (ccTLDAcc, ccTLD) => {
+ const ccTLDDomain = getDomain(ccTLD) || '';
+
+ if (ccTLDDomain) {
+ ccTLDAcc[ccTLDDomain] = ccTLDParent;
+ }
+
+ return ccTLDAcc;
+ },
+ {} as {
+ [ccTLD: string]: string;
+ }
+ );
+
+ return { ...acc, ...ccTLDsObj };
+ },
+ {} as {
+ [ccTLD: string]: string;
+ }
+ );
+
+ if (ccTLDs[domain]) {
+ ccTLDParentURL = ccTLDs[domain];
+ return true;
+ }
+
+ return false;
}
+ );
+
+ if (ccTLDParentURL && res) {
+ res.ccTLDParent = ccTLDParentURL;
+ }
- return domain ? rwsDomains.includes(domain) : false;
- });
+ return res;
};
export default findRWSURLSets;
diff --git a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx
index 64ecd82ce..e9180e35b 100644
--- a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx
+++ b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx
@@ -110,18 +110,28 @@ const Insights = () => {
{!insightsData.primary ? (
<>
- {Object.entries(
- insightsData.relatedWebsiteSet?.rationaleBySite || {}
- )
- .filter(
- ([domain]) => getDomain(domain) === insightsData.domain
- )
- .map(([domain, value]) => (
-
- Rationale:{' '}
- {value as string}
-
- ))}
+ {insightsData.isccTLD ? (
+
+ This site is a ccTLD of{' '}
+ {insightsData.relatedWebsiteSet?.ccTLDParent}.
+
+ ) : (
+ <>
+ {Object.entries(
+ insightsData.relatedWebsiteSet?.rationaleBySite || {}
+ )
+ .filter(
+ ([domain]) =>
+ getDomain(domain) === insightsData.domain
+ )
+ .map(([domain, value]) => (
+
+ Rationale:{' '}
+ {value as string}
+
+ ))}
+ >
+ )}
>
) : (
diff --git a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/checkURLInRWS.ts b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/checkURLInRWS.ts
index a5245caee..d45d6776b 100644
--- a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/checkURLInRWS.ts
+++ b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/checkURLInRWS.ts
@@ -24,21 +24,23 @@ import { getDomain } from 'tldts';
*/
import fetchRWSInfo from '../../../../../../../utils/fetchRWSInfo';
import getInspectedTabDomain from './getInspectedTabDomain';
-import type { RelatedWebsiteSetType } from '../../../../../../../@types';
-import findRWSURLSets from '../../../../../../../utils/findRWSURLSets';
+import findRWSURLSets, {
+ type RWSSetOutputType,
+} from '../../../../../../../utils/findRWSURLSets';
export type CheckURLInRWSOutputType = {
isURLInRWS: boolean;
primary?: boolean;
domain?: string;
- relatedWebsiteSet?: RelatedWebsiteSetType;
+ isccTLD?: boolean;
+ relatedWebsiteSet?: RWSSetOutputType;
};
const checkURLInRWS = async () => {
const tabDomain = (await getInspectedTabDomain()) || '';
- const rwsSets: RelatedWebsiteSetType[] = (await fetchRWSInfo()).sets || [];
+ const rwsSets: RWSSetOutputType[] = (await fetchRWSInfo()).sets || [];
- const urlInRWS: RelatedWebsiteSetType | undefined = findRWSURLSets(
+ const urlInRWS: RWSSetOutputType | undefined = findRWSURLSets(
tabDomain,
rwsSets
);
@@ -58,6 +60,15 @@ const checkURLInRWS = async () => {
} as CheckURLInRWSOutputType;
}
+ if (urlInRWS.ccTLDParent) {
+ return {
+ isURLInRWS: true,
+ isccTLD: true,
+ domain: tabDomain,
+ relatedWebsiteSet: urlInRWS,
+ } as CheckURLInRWSOutputType;
+ }
+
return {
isURLInRWS: true,
primary: false,
From b6ea972c7aca3dc46cc52dd7251a835bbc92d668 Mon Sep 17 00:00:00 2001
From: Mayank Rana
Date: Fri, 23 Feb 2024 20:56:08 +0530
Subject: [PATCH 2/3] ref: wrap url with anchor tag
---
.../relatedWebsiteSets/insights/index.tsx | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx
index e9180e35b..1b1935d66 100644
--- a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx
+++ b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/index.tsx
@@ -113,7 +113,16 @@ const Insights = () => {
{insightsData.isccTLD ? (
This site is a ccTLD of{' '}
- {insightsData.relatedWebsiteSet?.ccTLDParent}.
+
+ {insightsData.relatedWebsiteSet?.ccTLDParent}
+
+ .
) : (
<>
From df9ee327d21876e21fe4a4183fc244ad923719b7 Mon Sep 17 00:00:00 2001
From: Mayank Rana
Date: Fri, 23 Feb 2024 20:57:55 +0530
Subject: [PATCH 3/3] test: add testscases for ccTlds
---
.../insights/utils/tests/checkURLInRWS.ts | 125 ++++++++++++++----
1 file changed, 101 insertions(+), 24 deletions(-)
diff --git a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/tests/checkURLInRWS.ts b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/tests/checkURLInRWS.ts
index 48dec3dca..2871557d0 100644
--- a/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/tests/checkURLInRWS.ts
+++ b/packages/extension/src/view/devtools/components/siteBoundaries/relatedWebsiteSets/insights/utils/tests/checkURLInRWS.ts
@@ -20,61 +20,138 @@
import checkURLInRWS from '../checkURLInRWS';
describe('checkURLInRWS', () => {
- beforeAll(() => {
- globalThis.chrome = {
- devtools: {
- inspectedWindow: {
- tabId: 1,
+ globalThis.chrome = {
+ devtools: {
+ inspectedWindow: {
+ tabId: 1,
+ },
+ },
+ tabs: {
+ get: () => ({ url: 'https://hindustantimes.com' }),
+ },
+ runtime: {
+ getURL: () => 'data/related_website_sets.json',
+ },
+ } as unknown as typeof chrome;
+
+ globalThis.fetch = (): Promise =>
+ Promise.resolve({
+ json: () =>
+ Promise.resolve({
+ sets: [
+ {
+ contact: 'prashant.tiwari@htdigital.in',
+ primary: 'https://hindustantimes.com',
+ associatedSites: ['https://livemint.com'],
+ rationaleBySite: {
+ 'https://livemint.com': 'Specialized Platform for economics',
+ },
+ },
+ ],
+ }),
+ } as Response);
+
+ test('should return true if the tab domain is the same as the primary domain', async () => {
+ const result = await checkURLInRWS();
+
+ expect(result).toEqual({
+ isURLInRWS: true,
+ primary: true,
+ domain: 'hindustantimes.com',
+ relatedWebsiteSet: {
+ contact: 'prashant.tiwari@htdigital.in',
+ primary: 'https://hindustantimes.com',
+ associatedSites: ['https://livemint.com'],
+ rationaleBySite: {
+ 'https://livemint.com': 'Specialized Platform for economics',
},
},
+ });
+ });
+
+ test('should return false if tab domain not present in RWS', async () => {
+ globalThis.chrome = {
+ ...globalThis.chrome,
tabs: {
- get: () => ({ url: 'https://hindustantimes.com' }),
+ get: () => ({ url: 'https://indianexpress.com' }),
},
- runtime: {
- getURL: () => 'data/related_website_sets.json',
+ } as unknown as typeof chrome;
+
+ const result = await checkURLInRWS();
+
+ expect(result).toEqual({
+ isURLInRWS: false,
+ });
+ });
+
+ test('should return true if the tab domain is a ccTLD', async () => {
+ globalThis.chrome = {
+ ...globalThis.chrome,
+ tabs: {
+ get: () => ({ url: 'https://mercadolibre.com.ec' }),
},
} as unknown as typeof chrome;
+
globalThis.fetch = (): Promise =>
Promise.resolve({
json: () =>
Promise.resolve({
sets: [
{
- contact: 'prashant.tiwari@htdigital.in',
- primary: 'https://hindustantimes.com',
- associatedSites: ['https://livemint.com'],
- rationaleBySite: {
- 'https://livemint.com': 'Specialized Platform for economics',
+ contact: 'infraestructura@mercadolibre.com',
+ primary: 'https://mercadolibre.com',
+ associatedSites: [],
+ rationaleBySite: {},
+ ccTLDs: {
+ 'https://mercadolibre.com': [
+ 'https://mercadolibre.com.ar',
+ 'https://mercadolibre.com.mx',
+ 'https://mercadolibre.com.bo',
+ 'https://mercadolibre.cl',
+ 'https://mercadolibre.com.co',
+ 'https://mercadolibre.co.cr',
+ 'https://mercadolibre.com.do',
+ 'https://mercadolibre.com.ec',
+ ],
},
},
],
}),
} as Response);
- });
- test('should return true if the tab domain is the same as the primary domain', async () => {
const result = await checkURLInRWS();
expect(result).toEqual({
isURLInRWS: true,
- primary: true,
- domain: 'hindustantimes.com',
+ isccTLD: true,
+ domain: 'mercadolibre.com.ec',
relatedWebsiteSet: {
- contact: 'prashant.tiwari@htdigital.in',
- primary: 'https://hindustantimes.com',
- associatedSites: ['https://livemint.com'],
- rationaleBySite: {
- 'https://livemint.com': 'Specialized Platform for economics',
+ contact: 'infraestructura@mercadolibre.com',
+ primary: 'https://mercadolibre.com',
+ ccTLDParent: 'https://mercadolibre.com',
+ associatedSites: [],
+ rationaleBySite: {},
+ ccTLDs: {
+ 'https://mercadolibre.com': [
+ 'https://mercadolibre.com.ar',
+ 'https://mercadolibre.com.mx',
+ 'https://mercadolibre.com.bo',
+ 'https://mercadolibre.cl',
+ 'https://mercadolibre.com.co',
+ 'https://mercadolibre.co.cr',
+ 'https://mercadolibre.com.do',
+ 'https://mercadolibre.com.ec',
+ ],
},
},
});
});
- test('should return false if tab domain not present in RWS', async () => {
+ test('should return false if the tab domain is not a ccTLD', async () => {
globalThis.chrome = {
...globalThis.chrome,
tabs: {
- get: () => ({ url: 'https://indianexpress.com' }),
+ get: () => ({ url: 'https://mercadolibre.com.arg' }),
},
} as unknown as typeof chrome;