Skip to content

Commit dcba1f1

Browse files
authored
docs: add js doc (#6)
2 parents 376a791 + 1b65966 commit dcba1f1

File tree

10 files changed

+148
-24
lines changed

10 files changed

+148
-24
lines changed

src/bg/action.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import { matcher, runtimeMapUrl } from './bg';
44

55
const replacedUrlMatcher = new RegExp(`^${runtimeMapUrl}\?`);
66

7-
browserAction.onClicked.addListener(async (tab: Tabs.Tab) => {
7+
/**
8+
* Async function to react to clicks on the browser action icon.
9+
* Takes the current tab, takes its hostname and inverts the state of the hostname.
10+
*
11+
* Requests all frames from the current tab, filters them for extension Leaflet frames and Maps frames.
12+
* Reloads the full tab on extension Leaflet frame match.
13+
* If no extension Leaflet frames were found it just reloads the Maps frames
14+
* @param tab Currently active tab
15+
*/
16+
async function actionClick(tab: Tabs.Tab): Promise<void> {
817
if (!tab.url || !tab.id) return;
918

1019
let hostname = getHostname(tab.url);
@@ -26,4 +35,6 @@ browserAction.onClicked.addListener(async (tab: Tabs.Tab) => {
2635
code: 'document.location.reload();',
2736
});
2837
});
29-
});
38+
}
39+
40+
browserAction.onClicked.addListener(actionClick);

src/bg/bg.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export const matcher: RegExp = new RegExp(
1010
);
1111
export const runtimeMapUrl = runtime.getURL('map.html');
1212

13+
/**
14+
* Checks if `frames` send a request to Maps.
15+
* If they do and the extension isn't disabled for the current site, then the request is redirected to the extension leaflet map with all URL search params.
16+
* Else the request is'nt blocked.
17+
* @param req Web Request from frame
18+
* @returns Redirect to extension map or pass through if extension disabled for website
19+
*/
1320
function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.BlockingResponse {
1421
// TODO: check if originUrl always matches current tab url -> e.g. in frames with subframes
1522
if (req.originUrl && req.url.match(matcher)) {
@@ -22,6 +29,7 @@ function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.Blocki
2229
return {};
2330
}
2431

32+
// Listens to web requests from frames, redirects when fitting `matcher`
2533
webRequest.onBeforeRequest.addListener(
2634
redirect,
2735
{
@@ -40,5 +48,5 @@ tabs.onActivated.addListener(updateActiveTabIcon);
4048
// listen for window switching
4149
windows.onFocusChanged.addListener(updateActiveTabIcon);
4250

43-
// run at startup
51+
// update icon at startup
4452
updateActiveTabIcon();

src/bg/utils/actionIcon.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { browserAction, tabs } from 'webextension-polyfill';
22
import { disabledHosts, getHostname } from './storage';
33

4-
export function updateIcon(hostname: string) {
4+
/**
5+
* Updates the action icon
6+
* @param hostname Hostname
7+
*/
8+
export function updateIcon(hostname: string): void {
59
let disabled = disabledHosts.includes(hostname);
610

711
browserAction.setIcon({
@@ -17,7 +21,10 @@ export function updateIcon(hostname: string) {
1721
});
1822
}
1923

20-
export async function updateActiveTabIcon() {
24+
/**
25+
* Async function to update the icon of the currently active tab. Uses `updateIcon` internally
26+
*/
27+
export async function updateActiveTabIcon(): Promise<void> {
2128
let browserTabs = await tabs.query({ active: true, currentWindow: true });
2229

2330
let tab = browserTabs[0];

src/bg/utils/domainEnds.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Domain ends of google
12
export const domainEnds: string[] = [
23
'com',
34
'ac',

src/bg/utils/storage.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@ import { updateIcon } from './actionIcon';
33

44
export const KEY_DISABLED_HOSTS = 'disabled_hosts';
55

6+
// Listens to changes on the storage. Updates disabled hosts list, if stored list changes
67
export let disabledHosts: string[] = await getDisabledHosts();
78
storage.local.onChanged.addListener((changes) => {
89
if (KEY_DISABLED_HOSTS in changes) {
910
disabledHosts = changes[KEY_DISABLED_HOSTS].newValue ?? [];
1011
}
1112
});
1213

14+
/**
15+
* Async function to get the list of disabled hostnames
16+
* @returns List of disabled hostnames
17+
*/
1318
async function getDisabledHosts(): Promise<string[]> {
1419
return (await storage.local.get(KEY_DISABLED_HOSTS))[KEY_DISABLED_HOSTS] ?? [];
1520
}
1621

22+
/**
23+
* Async function to invert the state of a hostname.
24+
* Adds new entry if not disabled, removes entry, if already disabled
25+
* @param hostname Hostname to invert the state of
26+
*/
1727
export async function invertHostState(hostname: string): Promise<void> {
1828
if (disabledHosts.includes(hostname)) {
1929
disabledHosts.splice(disabledHosts.indexOf(hostname), 1);
@@ -28,6 +38,11 @@ export async function invertHostState(hostname: string): Promise<void> {
2838
updateIcon(hostname);
2939
}
3040

41+
/**
42+
* Retrieves the hostname from a URL
43+
* @param url Full URL string
44+
* @returns Hostname string
45+
*/
3146
export function getHostname(url: string): string {
3247
url = url.replace(/^\w+:\/\//, '');
3348
url = url.split(/[\/#\?]/, 1)[0];

src/map/utils/parseDMS.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
/**
2+
* Converts DMS coordinates to Lat and Lon
3+
* @param input String containing DMS coordinates
4+
* @returns Array containing Latitude and Longitude
5+
*/
16
export function parseDMS(input: string): [number, number] {
27
let parts: string[] = input.split(/[^\d\w\.]+/);
38
let lat = convertDMSToDD(parts.slice(0, 4));
4-
let lng = convertDMSToDD(parts.slice(4));
9+
let lon = convertDMSToDD(parts.slice(4));
510

6-
return [lat, lng];
11+
return [lat, lon];
712
}
8-
9-
function convertDMSToDD(dmsd: string[]): number {
10-
const [degrees, minutes, seconds, direction] = dmsd;
13+
/**
14+
* Converts DMS part to Lat/Lon
15+
* @param dms Array of four strings representing: Degree Minutes Seconds Direction
16+
* @returns DMS part converted to Latitude / Longitude
17+
*/
18+
function convertDMSToDD(dms: string[]): number {
19+
const [degrees, minutes, seconds, direction] = dms;
1120
let dd = Number(degrees) + Number(minutes) / 60 + Number(seconds) / (60 * 60);
1221

1322
if (direction === 'S' || direction === 'W') {

src/map/utils/parsePB.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
export type TileType = 'roadmap' | 'satellite';
22
export const tileTypes: TileType[] = ['roadmap', 'satellite'];
33

4+
/**
5+
* Takes one bang operator and decodes it.
6+
*
7+
* Possible types are:
8+
* - `s`: string
9+
* - `v`: timestamp => keep as string
10+
* - `b`: boolean? / byte? => keep as string
11+
* - `f`: float
12+
* - `d`: double
13+
* - `i`: int
14+
* - `m`: matrix => length as int
15+
* - `e`: enum
16+
* - `z`: base64 encoded coordinates => Degrees Minutes Seconds Direction
17+
*
18+
* Unknown types are kept as string.
19+
*
20+
* @param item One bang operator with the structure: position, one character (data type), encoded data
21+
* @returns Array of two items. First is the decoded result. Second describes if the result is a new matrix.
22+
*/
423
function convertType(item: string): [string | TileType | number, boolean] {
524
item = item.replace(/^\d+/, '');
625
const type: string = item.charAt(0);
@@ -29,6 +48,28 @@ function convertType(item: string): [string | TileType | number, boolean] {
2948
return [val, type === 'm'];
3049
}
3150

51+
/**
52+
* Half iterative half recursive function that converts an array of split hashbangs and converts it into a fitting representation.
53+
* Multiple nested arrays possible.
54+
*
55+
* Example input:
56+
* ```ts
57+
* TODO: ['', '', '']
58+
* ```
59+
*
60+
* Example result:
61+
* ```ts
62+
* TODO:
63+
* ```
64+
*
65+
* References:
66+
* - https://andrewwhitby.com/2014/09/09/google-maps-new-embed-format/
67+
* - https://blog.themillhousegroup.com/2016/08/deep-diving-into-google-pb-embedded-map.html
68+
* - https://stackoverflow.com/a/47042514
69+
* @param items Bang operators (e.g. `!1m13`) split into pieces at `!`
70+
* @param out Array for top and recursion levels to save results and return
71+
* @returns Filled `out` array with bang operators converted into readable format
72+
*/
3273
export function parsePB(items: string[], out: any[] = []): any[] {
3374
let i = 0;
3475
while (i < items.length) {

src/map/utils/read.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export type MapData = {
2424
markers?: Marker[];
2525
};
2626

27+
/**
28+
* Decodes the `pb` parameter with the help of `parsePB` and `readQ`
29+
* @param param Content of the `pb` parameter as a string
30+
* @returns MapData with area, zoom, tile type and markers
31+
*/
2732
export async function readPB(param: string): Promise<MapData> {
28-
// https://andrewwhitby.com/2014/09/09/google-maps-new-embed-format/
29-
// https://blog.themillhousegroup.com/2016/08/deep-diving-into-google-pb-embedded-map.html
30-
// https://stackoverflow.com/a/47042514
31-
3233
let mapData: MapData = {
3334
markers: [],
3435
};
@@ -47,7 +48,7 @@ export async function readPB(param: string): Promise<MapData> {
4748
if (typeof currMarkers !== 'string') {
4849
for (let markers of currMarkers[0] as string[]) {
4950
if (markers.match(cidMatch)) {
50-
// cid
51+
// TODO: understand CID
5152
console.log(markers);
5253
} else if (markers.match(dmsMatch)) {
5354
let [lat, lon] = parseDMS(markers);
@@ -74,9 +75,14 @@ export async function readPB(param: string): Promise<MapData> {
7475
return mapData;
7576
}
7677

78+
/**
79+
* Makes a request to the Nominatim API to get the coordinates of an address
80+
*
81+
* Reference: https://medium.com/@sowmyaaji/how-to-build-a-backend-with-jquery-and-add-a-leaflet-js-frontend-e77f2079c852
82+
* @param addr An address or place
83+
* @returns The Latitude and Logitude of the address or place
84+
*/
7785
export async function readQ(addr: string): Promise<Marker | null> {
78-
// https://medium.com/@sowmyaaji/how-to-build-a-backend-with-jquery-and-add-a-leaflet-js-frontend-e77f2079c852
79-
8086
let uri = encodeURI(nominatimQ + addr);
8187
let res = await fetch(uri);
8288

src/map/utils/zoom.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
// https://groups.google.com/g/google-earth-browser-plugin/c/eSL9GlAkWBk/m/T4mdToJz_FgJ
2-
31
const factor: number = 35200000;
42
const precision: number = 10;
53

4+
/**
5+
* Converts *altitude over the map* to *zoom level of the map*
6+
*
7+
* Reference: https://groups.google.com/g/google-earth-browser-plugin/c/eSL9GlAkWBk/m/T4mdToJz_FgJ
8+
* @param alt Altitude as number
9+
* @returns Zoom level between 0 and 19
10+
*/
611
export function getMapZoom(alt: number): number {
712
let zoom = Math.log2(factor / alt) * 1.225;
813
zoom = Math.round((zoom + Number.EPSILON) * precision) / precision;

src/options/options.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ import {
88

99
const table = document.querySelector('.table')!;
1010

11-
function buildEntries() {
11+
/**
12+
* (Re)Builds the list of diasabled hostnames
13+
*/
14+
function buildEntries(): void {
1215
table.innerHTML = '';
1316
disabledHosts.forEach(createEntry);
1417
}
1518

16-
async function addEntry() {
19+
/**
20+
* Async function to add a hostname (from the form / URL Search Params) to the displayed list and the storage list of disabled hosts
21+
* If the entry is already present in the stored hosts, no entry is added to the display list
22+
*/
23+
async function addEntry(): Promise<void> {
1724
const search = new URLSearchParams(document.location.search);
1825
let hostname = search.get('hostname');
1926
if (hostname === null) return;
@@ -25,7 +32,11 @@ async function addEntry() {
2532
createEntry(hostname);
2633
}
2734

28-
function createEntry(hostname: string) {
35+
/**
36+
* Creates a new entry for the displayed list of disabled hostnames and appends it to the view
37+
* @param hostname Hostname to add to the list
38+
*/
39+
function createEntry(hostname: string): void {
2940
const div = document.createElement('div');
3041

3142
let span = document.createElement('span');
@@ -38,7 +49,12 @@ function createEntry(hostname: string) {
3849
table.appendChild(div);
3950
}
4051

41-
async function removeEntry(click: MouseEvent) {
52+
/**
53+
* Async funtion to remove an entry at click of its button.
54+
* Takes the index in the table to remove it from the list of stored hostnames
55+
* @param click Button click
56+
*/
57+
async function removeEntry(click: MouseEvent): Promise<void> {
4258
let target: EventTarget | null = click.target;
4359
if (target === null) return;
4460

@@ -48,7 +64,12 @@ async function removeEntry(click: MouseEvent) {
4864
await invertHostState(disabledHosts[index]);
4965
}
5066

51-
function getIndex(button: HTMLButtonElement) {
67+
/**
68+
* Gets the index of a list entry using its clicked button
69+
* @param button Button that was clicked to remove an entry
70+
* @returns Index of the list entry
71+
*/
72+
function getIndex(button: HTMLButtonElement): number {
5273
let div: HTMLDivElement = button.parentElement as HTMLDivElement;
5374
if (div === null) return -1;
5475

0 commit comments

Comments
 (0)