Skip to content

Commit

Permalink
fix: WIP: crrently one directional, reload frames instead of tab
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Apr 16, 2023
1 parent 4245965 commit 6d9cebe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"permissions": [
"tabs",
"webNavigation",
"storage",
"webRequest",
"webRequestBlocking",
Expand Down
20 changes: 16 additions & 4 deletions src/bg/action.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { browserAction, tabs, type Tabs } from 'webextension-polyfill';
import { browserAction, webNavigation, type Tabs, tabs } from 'webextension-polyfill';
import { getHostname, invertHostState } from './utils/storage';
import { updateIcon } from './utils/actionIcon';
import { matcher, runtimeMapUrl } from './bg';

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

browserAction.onClicked.addListener(async (tab: Tabs.Tab) => {
if (!tab.url) return;
if (!tab.url || !tab.id) return;

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

updateIcon(hostname);

// TODO: reload only frames (that have gmaps url), not full page
tabs.reload(tab.id, { bypassCache: true });
let frames = (await webNavigation.getAllFrames({ tabId: tab.id })) ?? [];
frames = frames.filter((frame) => {
return frame.url.match(matcher) || frame.url.match(replacedUrlMatcher);
});
frames.forEach((frame) => {
// TODO: currently only works "from maps to osm" and "not from osm to maps" => fix it...
tabs.executeScript(tab.id, {
frameId: frame.frameId,
code: 'document.location.reload();',
});
});
});
6 changes: 4 additions & 2 deletions src/bg/bg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ const patterns: string[] = [
];

const gLocales: string = ['com', 'de'].join('|'); // TODO: collect more locales
const matcher: RegExp = new RegExp(
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');

function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.BlockingResponse {
// TODO: check if originUrl always matches current tab url -> e.g. in frames with subframes
if (req.originUrl && req.url.match(matcher)) {
if (!disabledHosts.includes(getHostname(req.originUrl))) {
return {
redirectUrl: runtime.getURL('map.html?' + req.url.split('?')[1]),
redirectUrl: runtimeMapUrl + '?' + req.url.split('?')[1],
};
}
}
Expand Down

0 comments on commit 6d9cebe

Please sign in to comment.