blob: 6e410afae1ca9a941fda3af1341699da05f1dec9 [file] [log] [blame]
Adrià Vilanova Martínez9f03abd2022-06-02 00:51:00 +02001import {DataType} from '../background';
2
3export default class URLFactory {
4 static getTranslationURL(
5 lang: string, info: chrome.contextMenus.OnClickData,
6 dataType: DataType) {
7 switch (dataType) {
8 case DataType.DataTypeText:
9 return URLFactory.getTranslationURLForText(lang, info.selectionText);
10
11 case DataType.DataTypeURL:
12 return URLFactory.getTranslationURLForURL(lang, info.linkUrl);
13
14 default:
15 console.error('Can\'t return translation URL for unknown data type.');
16 return 'about:blank?translate_selected_text_error';
17 }
18 }
19
20 static getTranslationURLForText(lang: string, text: string): string {
21 const params = new URLSearchParams({
22 sl: 'auto',
23 tl: lang,
24 text: text,
25 op: 'translate',
26 });
27 return 'https://translate.google.com/?' + params.toString();
28 }
29
30 static getTranslationURLForURL(lang: string, url: string): string {
31 const params = new URLSearchParams({
32 sl: 'auto',
33 tl: lang,
34 u: url,
35 });
36 return 'https://translate.google.com/translate?' + params.toString();
37 }
38}