Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reload maps frames only / reload tab on replaced map (for now) #4

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 21 additions & 4 deletions src/bg/action.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
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 })) ?? [];

// matches osm frames (just reloading the frame ist not working for some reason, so in case of replaced maps the whole tab is reloaded)
if (frames.some((frame) => frame.url.match(replacedUrlMatcher))) {
tabs.reload(tab.id, { bypassCache: true });
return;
}

// matches maps frames (finds the frames that have maps and reloads only them)
frames = frames.filter((frame) => frame.url.match(matcher));
frames.forEach((frame) => {
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