Skip to content

Commit faa7327

Browse files
committed
docs: adds some documentation
1 parent 3747239 commit faa7327

File tree

10 files changed

+128
-13
lines changed

10 files changed

+128
-13
lines changed

src/bg/action.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { matcher, runtimeMapUrl } from './bg';
44

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

7+
/**
8+
*
9+
*/
710
browserAction.onClicked.addListener(async (tab: Tabs.Tab) => {
811
if (!tab.url || !tab.id) return;
912

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { browserAction, tabs } from 'webextension-polyfill';
22
import { disabledHosts, getHostname } from './storage';
33

4+
/**
5+
*
6+
* @param hostname
7+
*/
48
export function updateIcon(hostname: string) {
59
let disabled = disabledHosts.includes(hostname);
610

@@ -17,6 +21,9 @@ export function updateIcon(hostname: string) {
1721
});
1822
}
1923

24+
/**
25+
*
26+
*/
2027
export async function updateActiveTabIcon() {
2128
let browserTabs = await tabs.query({ active: true, currentWindow: true });
2229

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@ import { updateIcon } from './actionIcon';
33

44
export const KEY_DISABLED_HOSTS = 'disabled_hosts';
55

6+
//
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+
*
16+
* @returns
17+
*/
1318
async function getDisabledHosts(): Promise<string[]> {
1419
return (await storage.local.get(KEY_DISABLED_HOSTS))[KEY_DISABLED_HOSTS] ?? [];
1520
}
1621

22+
/**
23+
*
24+
* @param hostname
25+
*/
1726
export async function invertHostState(hostname: string): Promise<void> {
1827
if (disabledHosts.includes(hostname)) {
1928
disabledHosts.splice(disabledHosts.indexOf(hostname), 1);
@@ -28,6 +37,11 @@ export async function invertHostState(hostname: string): Promise<void> {
2837
updateIcon(hostname);
2938
}
3039

40+
/**
41+
*
42+
* @param url
43+
* @returns
44+
*/
3145
export function getHostname(url: string): string {
3246
url = url.replace(/^\w+:\/\//, '');
3347
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+
*
29+
* @param param
30+
* @returns
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
const factor: number = 35200000;
44
const precision: number = 10;
55

6+
/**
7+
*
8+
* @param alt
9+
* @returns
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import {
88

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

11+
/**
12+
*
13+
*/
1114
function buildEntries() {
1215
table.innerHTML = '';
1316
disabledHosts.forEach(createEntry);
1417
}
1518

19+
/**
20+
*
21+
* @returns
22+
*/
1623
async function addEntry() {
1724
const search = new URLSearchParams(document.location.search);
1825
let hostname = search.get('hostname');
@@ -25,6 +32,10 @@ async function addEntry() {
2532
createEntry(hostname);
2633
}
2734

35+
/**
36+
*
37+
* @param hostname
38+
*/
2839
function createEntry(hostname: string) {
2940
const div = document.createElement('div');
3041

@@ -38,6 +49,11 @@ function createEntry(hostname: string) {
3849
table.appendChild(div);
3950
}
4051

52+
/**
53+
*
54+
* @param click
55+
* @returns
56+
*/
4157
async function removeEntry(click: MouseEvent) {
4258
let target: EventTarget | null = click.target;
4359
if (target === null) return;
@@ -48,6 +64,11 @@ async function removeEntry(click: MouseEvent) {
4864
await invertHostState(disabledHosts[index]);
4965
}
5066

67+
/**
68+
*
69+
* @param button
70+
* @returns
71+
*/
5172
function getIndex(button: HTMLButtonElement) {
5273
let div: HTMLDivElement = button.parentElement as HTMLDivElement;
5374
if (div === null) return -1;

0 commit comments

Comments
 (0)