forked from icd/rentgen
Zmigrowano wszystkie bezpośrednie wywołania browser.* API do zunifikowanej abstrakcji browserAPI, umożliwiając budowanie rozszerzenia zarówno dla Firefox (browser.browserAction) jak i Chrome (chrome.action) z jednego kodu źródłowego.
## Zmigrowane pliki aplikacji (4):
### 1. memory.ts
Dodano import:
- import browserAPI from ./lib/browser-api
Zastąpiono wywołania API:
- browser.browserAction.setBadgeText → browserAPI.badge.setBadgeText
- browser.browserAction.setTitle → browserAPI.badge.setTitle
- browser.browserAction.setBadgeBackgroundColor → browserAPI.badge.setBadgeBackgroundColor
- browser.webRequest.onBeforeRequest.addListener → browserAPI.webRequest.onBeforeRequest.addListener
- browser.webRequest.onBeforeSendHeaders.addListener → browserAPI.webRequest.onBeforeSendHeaders.addListener
- browser.cookies.getAll → browserAPI.cookies.getAll
- browser.cookies.remove → browserAPI.cookies.remove
- browser.extension.getBackgroundPage() → browserAPI.extension.getBackgroundPage()
Dodano obsługę null:
- Funkcja getMemory() sprawdza teraz czy getBackgroundPage() nie zwraca null
### 2. components/toolbar/toolbar.tsx
Dodano import:
- import browserAPI from ../../lib/browser-api
Zastąpiono wywołania API:
- browser.tabs.query → browserAPI.tabs.query
- browser.windows.WINDOW_ID_CURRENT → browserAPI.windows.WINDOW_ID_CURRENT
- browser.tabs.onUpdated.addListener → browserAPI.tabs.onUpdated.addListener
- browser.tabs.onUpdated.removeListener → browserAPI.tabs.onUpdated.removeListener
Zachowano całą funkcjonalność:
- Wszystkie sekcje UI (header, summary z licznikami, details, about, actions)
- Wszystkie hooki React i logika biznesowa
- Funkcje pomocnicze (getCurrentTab, isDomainHighlySuspicious, autoMark)
### 3. components/tab-dropdown.tsx
Zmieniono importy:
- Usunięto: import { Tab } from ../../util
- Dodano: import browserAPI, { Tab } from ../../lib/browser-api
Zastąpiono wywołania API:
- browser.tabs.query({ currentWindow: true }) → browserAPI.tabs.query({ currentWindow: true })
Poprawka typów:
- Typ Tab teraz pochodzi z browserAPI, zapewniając zgodność typów
### 4. util.ts
Dodano import:
- import browserAPI from ./lib/browser-api
Zastąpiono wywołania API:
- Typ Tab pochodzi teraz z browserAPI.tabs.query zamiast browser.tabs.query
- browser.tabs.query({ currentWindow: true }) → browserAPI.tabs.query({ currentWindow: true }) w funkcji getTabByID
Zachowano wszystkie funkcje:
- getshorthost, useEmitter, parseCookie, getTabByID
- parseToObject, isJSONObject, isURL, hyphenate, unique
- allSubhosts, reduceConcat, getDate, toBase64, makeThrottle
- isSameURL, isBase64, isBase64JSON
- flattenObject, flattenObjectEntries
- maskString, safeDecodeURIComponent, normalizeForClassname
- wordlist, dataLocationToText, downloadText
## Rozszerzenie abstrakcji browserAPI:
### lib/browser-api/types.ts
Dlaczego rozszerzono:
Początkowy minimalny interfejs RequestDetails był niewystarczający, ponieważ brakowało kluczowych właściwości wymaganych przez konstruktor ExtendedRequest. Gdy listenery webRequest są wywoływane, przekazują kompletny obiekt Request do ExtendedRequest, a nie tylko podstawowe szczegóły.
Co zostało dodane:
Pełny typ Request z util.ts, zawierający:
- Właściwości główne: requestId, tabId, url, method, type
- Nawigacja ramek: frameId, parentFrameId, documentUrl, originUrl
- Opcjonalne metadane: cookieStoreId, incognito, thirdParty, timeStamp
- Szczegóły żądania: requestHeaders, urlClassification, proxyInfo
Przyczyna źródłowa błędów TypeScript:
Konstruktor ExtendedRequest oczekiwał właściwości takich jak frameId, method,
originUrl, parentFrameId, documentUrl, urlClassification, etc. Minimalny
interfejs powodował błędy:
- Argument of type RequestDetails is not assignable to parameter of type Request
- Type RequestDetails is missing properties: frameId, method, originUrl, parentFrameId, and 4 more
Rozwiązanie:
Używając pełnej definicji typu Request, abstrakcja browserAPI poprawnie typuje callbacki webRequest, zapewniając bezpieczeństwo typów zarówno dla buildu Firefox jak i Chrome, przy zachowaniu kompatybilności z istniejącą implementacją ExtendedRequest.
Zmiana w RequestListener:
- Było: (details: RequestDetails) => void
- Jest: (details: Request) => void
## Wpływ zmian:
- memory.ts, toolbar.tsx, tab-dropdown.tsx, util.ts działają z TARGET=firefox i TARGET=chrome
- Zachowano bezpieczeństwo typów w całym kodzie
- Brak zmian funkcjonalnych - tylko warstwa abstrakcji
- Gotowość do kompatybilności z Chrome Manifest V3 (chrome.action vs browser.browserAction)
## Następne kroki:
- Aktualizacja esbuild.config.js dla budowania z TARGET=chrome do dist-chrome/
- Aktualizacja manifestu Chrome zgodnie z regułami Manifest v3
- Skrypt konwertujący SVG na PNG dla Chrome
- Testowanie rozszerzenia w przeglądarce Chrome
270 lines
11 KiB
TypeScript
270 lines
11 KiB
TypeScript
import React, { Fragment } from 'react';
|
|
import ReactDOM from 'react-dom';
|
|
import { getMemory } from '../../memory';
|
|
import { useEmitter, getshorthost } from '../../util';
|
|
import browserAPI from '../../lib/browser-api';
|
|
|
|
async function getCurrentTab() {
|
|
const [tab] = await browserAPI.tabs.query({
|
|
active: true,
|
|
windowId: browserAPI.windows.WINDOW_ID_CURRENT,
|
|
});
|
|
return tab;
|
|
}
|
|
|
|
function isDomainHighlySuspicious(domain: string) {
|
|
return (
|
|
domain.includes('facebook') ||
|
|
domain.includes('twitter') ||
|
|
domain.includes('linkedin') ||
|
|
false
|
|
);
|
|
}
|
|
|
|
const Toolbar = () => {
|
|
const [origin, setOrigin] = React.useState<string | null>(null);
|
|
const [eventCounts] = useEmitter(getMemory());
|
|
const [cookieDomainCopy, setCookieDomainCopy] = React.useState<string | null>(null);
|
|
const [_, setMarksOccurrence] = React.useState(false);
|
|
const [exposedOriginDomainCopy, setExposedOriginDomainCopy] = React.useState<string | null>(
|
|
null
|
|
);
|
|
|
|
const first_sentence_cookie =
|
|
'Strona dokonała zapisu i odczytu plików Cookie dla domen ';
|
|
const first_sentence_history =
|
|
'Część informacji o Twojej historii przeglądania została wysłana do ';
|
|
|
|
React.useEffect(() => {
|
|
const listener = async () => {
|
|
const tab = await getCurrentTab();
|
|
if (tab !== undefined && tab.url) {
|
|
const url = new URL(tab.url);
|
|
if (url.origin.startsWith('moz-extension')) {
|
|
return;
|
|
}
|
|
setOrigin(url.origin);
|
|
} else {
|
|
console.warn('Out of the tab scope');
|
|
}
|
|
};
|
|
browserAPI.tabs.onUpdated.addListener(listener);
|
|
listener();
|
|
return () => {
|
|
browserAPI.tabs.onUpdated.removeListener(listener);
|
|
};
|
|
});
|
|
|
|
React.useEffect(() => {
|
|
if (!origin) return;
|
|
|
|
const exposedOriginDomains = Object.values(getMemory().getClustersForOrigin(origin))
|
|
.filter((cluster) => cluster.exposesOrigin())
|
|
.sort((cluster1, cluster2) =>
|
|
isDomainHighlySuspicious(cluster1.id)
|
|
? -1
|
|
: isDomainHighlySuspicious(cluster2.id)
|
|
? 1
|
|
: 0
|
|
)
|
|
.map((cluster) => cluster.id);
|
|
|
|
setExposedOriginDomainCopy('');
|
|
|
|
switch (exposedOriginDomains.length) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
setExposedOriginDomainCopy(`${exposedOriginDomains[0]}.`);
|
|
break;
|
|
case 2:
|
|
setExposedOriginDomainCopy(
|
|
`${exposedOriginDomains[0]} oraz ${exposedOriginDomains[1]}.`
|
|
);
|
|
break;
|
|
case 3:
|
|
setExposedOriginDomainCopy(
|
|
`${exposedOriginDomains[0]}, ${exposedOriginDomains[1]} oraz ${exposedOriginDomains[2]}.`
|
|
);
|
|
break;
|
|
default:
|
|
setExposedOriginDomainCopy(
|
|
`${exposedOriginDomains[0]}, ${exposedOriginDomains[1]} (i ${
|
|
exposedOriginDomains.length - 2 < 2 ? 2 : exposedOriginDomains.length - 2
|
|
} innych).`
|
|
);
|
|
break;
|
|
}
|
|
}, [eventCounts['*'], origin]);
|
|
|
|
React.useEffect(() => {
|
|
if (!origin) return;
|
|
|
|
const cookieDomains = Object.values(getMemory().getClustersForOrigin(origin))
|
|
.filter((cluster) => cluster.hasCookies())
|
|
.sort((cluster1, cluster2) =>
|
|
isDomainHighlySuspicious(cluster1.id)
|
|
? -1
|
|
: isDomainHighlySuspicious(cluster2.id)
|
|
? 1
|
|
: 0
|
|
)
|
|
.map((cluster) => cluster.id);
|
|
|
|
setCookieDomainCopy('');
|
|
|
|
switch (cookieDomains.length) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
setCookieDomainCopy(`${cookieDomains[0]}.`);
|
|
break;
|
|
case 2:
|
|
setCookieDomainCopy(`${cookieDomains[0]} oraz ${cookieDomains[1]}.`);
|
|
break;
|
|
case 3:
|
|
setCookieDomainCopy(
|
|
`${cookieDomains[0]}, ${cookieDomains[1]} oraz ${cookieDomains[2]}.`
|
|
);
|
|
break;
|
|
default:
|
|
setCookieDomainCopy(
|
|
`${cookieDomains[0]}, ${cookieDomains[1]} (i ${
|
|
cookieDomains.length - 2 < 2 ? 2 : cookieDomains.length - 2
|
|
} innych).`
|
|
);
|
|
break;
|
|
}
|
|
}, [eventCounts['*'], origin]);
|
|
|
|
const autoMark = () => {
|
|
Object.values(getMemory().getClustersForOrigin(origin || '')).forEach((cluster) =>
|
|
cluster.autoMark()
|
|
);
|
|
setMarksOccurrence(true);
|
|
};
|
|
|
|
return (
|
|
<div className="toolbar">
|
|
<header className={origin ? 'header' : 'header header--no-page'}>
|
|
<img src="../../assets/icon-addon.svg" height="24" />
|
|
<div className="webpage-metadata">
|
|
{origin ? (
|
|
<div className="webpage-metadata--hyperlink">{origin}</div>
|
|
) : (
|
|
<div>Rentgen - wtyczka do przeglądania</div>
|
|
)}
|
|
</div>
|
|
{origin ? (
|
|
<button
|
|
onClick={() => {
|
|
window.close();
|
|
}}
|
|
>
|
|
<img src="../../assets/icons/x_thick.svg" width="12" height="12" />
|
|
</button>
|
|
) : (
|
|
<a href="https://internet-czas-dzialac.pl">
|
|
<img src="/assets/icons/info_circle_outline.svg" width="20" height="20" />
|
|
</a>
|
|
)}
|
|
</header>
|
|
|
|
{origin ? (
|
|
<Fragment>
|
|
{' '}
|
|
<section className="summary">
|
|
<div className="counters-wrapper">
|
|
<div className="counters">
|
|
<div className="counter counter--cookies">
|
|
<img src="/assets/icons/cookie.svg#color" width="24" height="24" />
|
|
<span data-event={`${eventCounts['*']}`}>
|
|
{
|
|
Object.values(getMemory().getClustersForOrigin(origin)).filter(
|
|
(cluster) => cluster.hasCookies()
|
|
).length
|
|
}
|
|
</span>
|
|
</div>
|
|
<div className="counter counter--browser-history">
|
|
<img src="/assets/icons/warning.svg#color" width="24" height="24" />
|
|
<span data-event={`${eventCounts['*']}`}>
|
|
{
|
|
Object.values(getMemory().getClustersForOrigin(origin)).filter(
|
|
(cluster) => cluster.exposesOrigin()
|
|
).length
|
|
}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="big-counter" data-event={`${eventCounts['*']}`}>
|
|
{Object.values(getMemory().getClustersForOrigin(origin)).length}
|
|
</div>
|
|
</div>
|
|
<span className="notice">Liczba wykrytych domen podmiotów trzecich</span>
|
|
</section>
|
|
<section className="details">
|
|
{cookieDomainCopy ? (
|
|
<p
|
|
data-event={`${eventCounts['*']}`}
|
|
title={Object.values(getMemory().getClustersForOrigin(origin))
|
|
.filter((cluster) => cluster.hasCookies())
|
|
.map((domain) => domain.id)
|
|
.join(', ')}
|
|
>
|
|
{first_sentence_cookie}
|
|
<strong>{cookieDomainCopy}</strong>
|
|
</p>
|
|
) : null}
|
|
{exposedOriginDomainCopy ? (
|
|
<p
|
|
data-event={`${eventCounts['*']}`}
|
|
title={Object.values(getMemory().getClustersForOrigin(origin))
|
|
.filter((cluster) => cluster.exposesOrigin())
|
|
.map((domain) => domain.id)
|
|
.join(', ')}
|
|
>
|
|
{first_sentence_history}
|
|
<strong>{exposedOriginDomainCopy}</strong>
|
|
</p>
|
|
) : null}
|
|
</section>
|
|
{exposedOriginDomainCopy || cookieDomainCopy ? (
|
|
<Fragment>
|
|
<section className="about">
|
|
<p>
|
|
Takie przetwarzanie danych może być niezgodne z prawem.
|
|
Przejdź do analizy aby pomóc ustalić, czy ta strona nie narusza
|
|
RODO lub ustawy Prawo Komunikacji Elektronicznej.
|
|
</p>
|
|
</section>
|
|
<section className="actions">
|
|
<button
|
|
className="button button--report"
|
|
onClick={() => {
|
|
autoMark();
|
|
window.open(
|
|
`/components/sidebar/sidebar.html?origin=${origin}`,
|
|
'new_tab'
|
|
);
|
|
window.close();
|
|
}}
|
|
>
|
|
Przejdź do analizy
|
|
</button>
|
|
</section>
|
|
</Fragment>
|
|
) : (
|
|
<Fragment>
|
|
<section className="about about__no-errors">
|
|
<p>Nie znaleziono problemów na tej stronie.</p>
|
|
</section>
|
|
</Fragment>
|
|
)}
|
|
</Fragment>
|
|
) : null}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
ReactDOM.render(<Toolbar />, document.getElementById('toolbar')); |