Skip to content

Commit

Permalink
fix: can't redirect when URL has fragment
Browse files Browse the repository at this point in the history
closes #687
  • Loading branch information
ikemo3 committed Apr 30, 2023
1 parent f76a846 commit b95d0c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion apps/entry/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { OptionRepository } from "../repository/options";
import BlockDialog from "../content_script/dialog";
import { IBasicBlockMediator } from "../content_script/mediator";
import localizeHtmlPage from "../option/l10n";
import DocumentURL from "../values/document_url";

function getCurrentTab(): Promise<chrome.tabs.Tab> {
return new Promise((resolve, reject) => {
Expand All @@ -29,7 +30,12 @@ searchInEnglishButton.addEventListener("click", async () => {
const currentTab = await getCurrentTab();
const { url } = currentTab;

chrome.tabs.update(currentTab.id!, { url: `${url}&gl=us&hl=en` });
if (!url) {
return;
}

const documentUrl = new DocumentURL(url);
chrome.tabs.update(currentTab.id!, { url: documentUrl.buildSearchInEnglishURL() });
});

exceptIkagadesitakaButton.addEventListener("click", async () => {
Expand Down
8 changes: 8 additions & 0 deletions apps/values/document_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class DocumentURL {
isGoogleSearch(): boolean {
return !this.isGoogleSearchNewsTab() && !this.isGoogleSearchImageTab();
}

buildSearchInEnglishURL(): string {
const returnURL = new URL(this.url.toString());
const params = returnURL.searchParams;
params.set("gl", "us");
params.set("hl", "en");
return returnURL.toString();
}
}

export default DocumentURL;
16 changes: 16 additions & 0 deletions test/document_url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ describe("DocumentURL", () => {
expect(documentUrl.isGoogleSearchImageTab()).toBe(true);
});
});

describe("Search In English", () => {
it("normal", () => {
const documentUrl = new DocumentURL(GOOGLE_SEARCH_URL);
const expected = GOOGLE_SEARCH_URL + "&gl=us&hl=en";

expect(documentUrl.buildSearchInEnglishURL()).toBe(expected);
});

it("has fragment", () => {
const documentUrl = new DocumentURL("https://www.google.com/search?q=test&rlz=xxx&oq=test&aqs=yyy&sourceid=chrome&ie=UTF-8#bsht=zzz");
const expected = "https://www.google.com/search?q=test&rlz=xxx&oq=test&aqs=yyy&sourceid=chrome&ie=UTF-8&gl=us&hl=en#bsht=zzz";

expect(documentUrl.buildSearchInEnglishURL()).toBe(expected);
})
});

0 comments on commit b95d0c4

Please sign in to comment.