Skip to content

Commit

Permalink
lint: drop semicolon rule, cleanup vitest code
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Jun 25, 2024
1 parent f064723 commit 43913d6
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 293 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"endOfLine": "lf",
"bracketSameLine": true,
"bracketSpacing": true,
"semi": true,
"semi": false,
"trailingComma": "es5",
"singleQuote": true,
"arrowParens": "always",
Expand Down
14 changes: 7 additions & 7 deletions src/bg/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { browserAction, /*webNavigation,*/ type Tabs, tabs } from 'webextension-polyfill';
import { getHostname, invertHostState } from './utils/storage';
import { browserAction, /*webNavigation,*/ type Tabs, tabs } from 'webextension-polyfill'
import { getHostname, invertHostState } from './utils/storage'
//import { matcher as mapsUrlMatcher, runtimeMapUrl } from './bg';

//const replacedUrlMatcher = new RegExp(`^${runtimeMapUrl}\?`);
Expand All @@ -13,10 +13,10 @@ import { getHostname, invertHostState } from './utils/storage';
* @param tab Currently active tab
*/
async function actionClick(tab: Tabs.Tab): Promise<void> {
if (!tab.url || !tab.id) return;
if (!tab.url || !tab.id) return

let hostname = getHostname(tab.url);
await invertHostState(hostname);
let hostname = getHostname(tab.url)
await invertHostState(hostname)

/*
// TODO: try to only reload necessary parts!!!
Expand All @@ -28,7 +28,7 @@ async function actionClick(tab: Tabs.Tab): Promise<void> {
})
)
*/
tabs.reload(tab.id, { bypassCache: true });
tabs.reload(tab.id, { bypassCache: true })
}

browserAction.onClicked.addListener(actionClick);
browserAction.onClicked.addListener(actionClick)
28 changes: 14 additions & 14 deletions src/bg/bg.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { runtime, tabs, windows, webRequest, type WebRequest } from 'webextension-polyfill';
import { disabledHosts, getHostname } from './utils/storage';
import { updateActiveTabIcon } from './utils/actionIcon';
import { domainEnds } from './utils/domainEnds';
import { runtime, tabs, windows, webRequest, type WebRequest } from 'webextension-polyfill'
import { disabledHosts, getHostname } from './utils/storage'
import { updateActiveTabIcon } from './utils/actionIcon'
import { domainEnds } from './utils/domainEnds'

const gLocales: string = domainEnds.join('|'); // TODO: collect more locales
const gLocales: string = domainEnds.join('|') // TODO: collect more locales
export const matcher: RegExp = new RegExp(
// TODO: fix regex to fit more patterns
`^(https?:\/\/)?(maps\.google\.(${gLocales})\/maps.*\?.*output=embed|(www\.)?google\.(${gLocales})\/maps\/embed.*\?)`
);
export const runtimeMapUrl = runtime.getURL('map.html');
)
export const runtimeMapUrl = runtime.getURL('map.html')

/**
* Checks if `frames` send a request to Maps.
Expand All @@ -23,10 +23,10 @@ function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.Blocki
if (!disabledHosts.includes(getHostname(req.originUrl))) {
return {
redirectUrl: runtimeMapUrl + '?' + req.url.split('?')[1],
};
}
}
}
return {};
return {}
}

// Listens to web requests from frames, redirects when fitting `matcher`
Expand All @@ -37,16 +37,16 @@ webRequest.onBeforeRequest.addListener(
types: ['sub_frame'],
},
['blocking']
);
)

// listen to tab URL changes
tabs.onUpdated.addListener(updateActiveTabIcon);
tabs.onUpdated.addListener(updateActiveTabIcon)

// listen to tab switching
tabs.onActivated.addListener(updateActiveTabIcon);
tabs.onActivated.addListener(updateActiveTabIcon)

// listen for window switching
windows.onFocusChanged.addListener(updateActiveTabIcon);
windows.onFocusChanged.addListener(updateActiveTabIcon)

// update icon at startup
updateActiveTabIcon();
updateActiveTabIcon()
14 changes: 7 additions & 7 deletions src/bg/utils/actionIcon.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { browserAction, tabs } from 'webextension-polyfill';
import { disabledHosts, getHostname } from './storage';
import { browserAction, tabs } from 'webextension-polyfill'
import { disabledHosts, getHostname } from './storage'

/**
* Updates the action icon
* @param hostname Hostname
*/
export function updateIcon(hostname: string): void {
let disabled = disabledHosts.includes(hostname);
let disabled = disabledHosts.includes(hostname)

browserAction.setIcon({
path: !disabled
Expand All @@ -18,17 +18,17 @@ export function updateIcon(hostname: string): void {
48: '/icons/48-grey.png',
96: '/icons/96-grey.png',
},
});
})
}

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

let tab = browserTabs[0];
let tab = browserTabs[0]
if (tab && tab.url) {
updateIcon(getHostname(tab.url));
updateIcon(getHostname(tab.url))
}
}
2 changes: 1 addition & 1 deletion src/bg/utils/domainEnds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@ export const domainEnds: string[] = [
'co.za',
'co.zm',
'co.zw',
];
]
28 changes: 14 additions & 14 deletions src/bg/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { storage } from 'webextension-polyfill';
import { updateIcon } from './actionIcon';
import { storage } from 'webextension-polyfill'
import { updateIcon } from './actionIcon'

export const KEY_DISABLED_HOSTS = 'disabled_hosts';
export const KEY_DISABLED_HOSTS = 'disabled_hosts'

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

/**
* Async function to get the list of disabled hostnames
* @returns List of disabled hostnames
*/
async function getDisabledHosts(): Promise<string[]> {
return (await storage.local.get(KEY_DISABLED_HOSTS))[KEY_DISABLED_HOSTS] ?? [];
return (await storage.local.get(KEY_DISABLED_HOSTS))[KEY_DISABLED_HOSTS] ?? []
}

/**
Expand All @@ -26,16 +26,16 @@ async function getDisabledHosts(): Promise<string[]> {
*/
export async function invertHostState(hostname: string): Promise<void> {
if (disabledHosts.includes(hostname)) {
disabledHosts.splice(disabledHosts.indexOf(hostname), 1);
disabledHosts.splice(disabledHosts.indexOf(hostname), 1)
} else {
disabledHosts.push(hostname);
disabledHosts.push(hostname)
}

await storage.local.set({
[KEY_DISABLED_HOSTS]: disabledHosts,
});
})

updateIcon(hostname);
updateIcon(hostname)
}

/**
Expand All @@ -44,7 +44,7 @@ export async function invertHostState(hostname: string): Promise<void> {
* @returns Hostname string
*/
export function getHostname(url: string): string {
url = url.replace(/^\w+:\/\//, '');
url = url.split(/[\/#\?]/, 1)[0];
return url;
url = url.replace(/^\w+:\/\//, '')
url = url.split(/[\/#\?]/, 1)[0]
return url
}
54 changes: 27 additions & 27 deletions src/map/map.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import L from 'leaflet';
import 'leaflet-fullscreen';
import L from 'leaflet'
import 'leaflet-fullscreen'

import { readPB, readQ, type MapData } from './utils/read';
import { type TileType, tileTypes } from './utils/parsePB';
import { readPB, readQ, type MapData } from './utils/read'
import { type TileType, tileTypes } from './utils/parsePB'

type Tiles = {
[K in TileType]: {
layer: string;
attr: string;
};
};
layer: string
attr: string
}
}

// https://leaflet-extras.github.io/leaflet-providers/preview/
const tileProviders: Tiles = {
Expand All @@ -21,27 +21,27 @@ const tileProviders: Tiles = {
layer: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', // Esri.WorldImagery
attr: 'Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',
}, // TODO: add street layer etc to satellite
};
}

const gPos: string = 'pb';
const gQuery: string = 'q';
const gZoom: string = 'z';
const params: URLSearchParams = new URLSearchParams(document.location.search);
const gPos: string = 'pb'
const gQuery: string = 'q'
const gZoom: string = 'z'
const params: URLSearchParams = new URLSearchParams(document.location.search)

let mapData: MapData = {};
let mapData: MapData = {}

if (params.has(gPos)) {
mapData = await readPB(params.get(gPos) as string);
mapData = await readPB(params.get(gPos) as string)
} else if (params.has(gQuery)) {
let marker = await readQ(params.get(gQuery) as string);
let marker = await readQ(params.get(gQuery) as string)

if (marker) {
mapData.markers = [marker];
mapData.markers = [marker]
}
}

if (params.has(gZoom)) {
mapData.zoom = parseInt(params.get(gZoom) as string);
mapData.zoom = parseInt(params.get(gZoom) as string)
}

const map: L.Map = L.map('map', {
Expand All @@ -51,29 +51,29 @@ const map: L.Map = L.map('map', {
zoomSnap: 0.1,
zoomDelta: 0.5,
minZoom: 0.5,
});
})

if (mapData.markers?.length == 0 && mapData.area) {
map.setView([mapData.area.lat, mapData.area.lon]);
map.setView([mapData.area.lat, mapData.area.lon])
}

if (mapData.markers) {
if (mapData.markers.length === 1) {
let mapMarker = mapData.markers[0];
map.setView([mapMarker.lat, mapMarker.lon]);
let mapMarker = mapData.markers[0]
map.setView([mapMarker.lat, mapMarker.lon])
}

mapData.markers.forEach((marker) => {
let mapMarker = L.marker([marker.lat, marker.lon]).addTo(map);
mapMarker.bindPopup(marker.label, { closeButton: false }).openPopup();
});
let mapMarker = L.marker([marker.lat, marker.lon]).addTo(map)
mapMarker.bindPopup(marker.label, { closeButton: false }).openPopup()
})
}

L.tileLayer(tileProviders[mapData.tile || tileTypes[0]].layer, {
maxZoom: 19,
attribution: tileProviders[mapData.tile || tileTypes[0]].attr,
}).addTo(map);
}).addTo(map)

L.control.scale().addTo(map);
L.control.scale().addTo(map)
// TODO: fix layer loading
//L.control.layers({}, {}, { hideSingleBase: true }).addTo(map)
16 changes: 8 additions & 8 deletions src/map/utils/parseDMS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
* @returns Array containing Latitude and Longitude
*/
export function parseDMS(input: string): [number, number] {
let parts: string[] = input.split(/[^\d\w\.]+/);
let lat = convertDMSToDD(parts.slice(0, 4));
let lon = convertDMSToDD(parts.slice(4));
let parts: string[] = input.split(/[^\d\w\.]+/)
let lat = convertDMSToDD(parts.slice(0, 4))
let lon = convertDMSToDD(parts.slice(4))

return [lat, lon];
return [lat, lon]
}
/**
* Converts DMS part to Lat/Lon
* @param dms Array of four strings representing: Degree Minutes Seconds Direction
* @returns DMS part converted to Latitude / Longitude
*/
function convertDMSToDD(dms: string[]): number {
const [degrees, minutes, seconds, direction] = dms;
let dd = Number(degrees) + Number(minutes) / 60 + Number(seconds) / (60 * 60);
const [degrees, minutes, seconds, direction] = dms
let dd = Number(degrees) + Number(minutes) / 60 + Number(seconds) / (60 * 60)

if (direction === 'S' || direction === 'W') {
dd = dd * -1;
dd = dd * -1
} // Don't do anything for N or E
return dd;
return dd
}
Loading

0 comments on commit 43913d6

Please sign in to comment.