-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add auto defining page language and apply it to popoup tabs
- Loading branch information
Showing
17 changed files
with
195 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/background/shared/entities/language/currentPageLanguageStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { atom, computed, onMount } from 'nanostores' | ||
|
||
import { browser } from '@background/shared/browser' | ||
|
||
import { | ||
getLanguageFromPage, | ||
getLanguageFromPageResult, | ||
} from '@shared/features/note/AutoAddNewNote' | ||
|
||
import { TLanguageCode } from '@shared/entities/language' | ||
import { $notes } from '@shared/entities/note/model/store' | ||
import { $activeTab } from '@shared/entities/tab' | ||
|
||
import { PortEmitter } from '@shared/shared/browser/port' | ||
import { TTab } from '@shared/shared/browser/tabs' | ||
|
||
export const $currentPageLanguage = atom<TLanguageCode>('other') | ||
|
||
onMount($currentPageLanguage, () => { | ||
function getLanguage(activeTab: TTab) { | ||
if (activeTab.url.includes('chrome://')) return | ||
|
||
const port = new PortEmitter({ | ||
tabId: activeTab.id, | ||
connectInfo: { name: 'CurrentPage' }, | ||
}) | ||
|
||
getLanguageFromPage(port) | ||
|
||
getLanguageFromPageResult(port, (languageCode) => { | ||
$currentPageLanguage.set(languageCode) | ||
}) | ||
} | ||
|
||
const activeTab = $activeTab.get() | ||
if (activeTab) getLanguage(activeTab) | ||
|
||
browser.tabs.onActivated.addListener((activeTab) => { | ||
if (activeTab.data) getLanguage(activeTab.data) | ||
}) | ||
}) | ||
|
||
export const $latestNote = computed( | ||
[$currentPageLanguage, $notes], | ||
(currentPageLanguage, notes) => { | ||
return notes[currentPageLanguage][0] | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as currentPageLanguageStore from './currentPageLanguageStore' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './model' |
30 changes: 30 additions & 0 deletions
30
src/popup/entities/language/model/currentPageLanguageStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { computed, task } from 'nanostores' | ||
|
||
import { | ||
getLanguageFromPage, | ||
getLanguageFromPageResult, | ||
} from '@shared/features/note/AutoAddNewNote' | ||
|
||
import { TLanguageCode } from '@shared/entities/language' | ||
import { $activeTab } from '@shared/entities/tab' | ||
|
||
import { PortEmitter } from '@shared/shared/browser/port' | ||
|
||
export const $currentPageLanguage = computed($activeTab, (activeTab) => { | ||
return task(async (): Promise<TLanguageCode> => { | ||
if (!activeTab?.id || activeTab.url.includes('chrome://')) return 'other' | ||
|
||
const port = new PortEmitter({ | ||
tabId: activeTab.id, | ||
connectInfo: { name: 'CurrentPage' }, | ||
}) | ||
|
||
getLanguageFromPage(port) | ||
|
||
return new Promise((resolve) => { | ||
getLanguageFromPageResult(port, (languageCode) => { | ||
resolve(languageCode) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as currentLanguagePageStore from './currentPageLanguageStore' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getDictionaryByPageUrl } from '@shared/entities/dictionary' | ||
|
||
import { languageStore } from '..' | ||
import { TLanguageCode } from './types' | ||
|
||
export const getLanguageCodeByPageUrl = (url: string): TLanguageCode => { | ||
const dictionary = getDictionaryByPageUrl(url) | ||
return dictionary ? dictionary.languageCode : 'other' | ||
} | ||
|
||
export const getLanguageCodeByPageMetaData = () => { | ||
const pageLanguageCode = document | ||
.getElementsByTagName('html')[0] | ||
.getAttribute('lang') as TLanguageCode | ||
|
||
return languageStore.$languageCodes.get().includes(pageLanguageCode) | ||
? pageLanguageCode | ||
: 'other' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,4 @@ | ||
import { DICTIONARIES } from '@shared/entities/dictionary' | ||
|
||
import { TLanguageCode } from './types' | ||
|
||
export type { TLanguageCode, ILanguage } from './types' | ||
export { LANGUAGES, LANGUAGE_CODES } from './languages' | ||
export * as languageStore from './store' | ||
|
||
export const getLanguageCodeByPageUrl = (url: string): TLanguageCode => { | ||
const languages = Object.entries(DICTIONARIES) | ||
|
||
let languageIndex = 0 | ||
while (languageIndex < languages.length) { | ||
const languageDictionaries = languages[languageIndex][1].dictionaries | ||
|
||
let dictionaryIndex = 0 | ||
while (dictionaryIndex < languageDictionaries.length) { | ||
const dictionary = languageDictionaries[dictionaryIndex] | ||
|
||
if (url.includes(dictionary.url)) { | ||
return languages[languageIndex][1].value | ||
} | ||
|
||
dictionaryIndex++ | ||
} | ||
languageIndex++ | ||
} | ||
|
||
return 'other' | ||
} | ||
export * from './helpers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters