fix: update chrome.i18n
for Manifest v3 (#39291)
fix: update chrome.i18n for Manifest v3
This commit is contained in:
parent
aaae2abf20
commit
303b707fbf
8 changed files with 197 additions and 46 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
// Copyright 2012 The Chromium Authors
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
@ -18,15 +18,20 @@
|
||||||
"name": "getAcceptLanguages",
|
"name": "getAcceptLanguages",
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"description": "Gets the accept-languages of the browser. This is different from the locale used by the browser; to get the locale, use $(ref:i18n.getUILanguage).",
|
"description": "Gets the accept-languages of the browser. This is different from the locale used by the browser; to get the locale, use $(ref:i18n.getUILanguage).",
|
||||||
"parameters": [
|
"parameters": [],
|
||||||
{
|
"returns_async": {
|
||||||
"type": "function",
|
"name": "callback",
|
||||||
"name": "callback",
|
"parameters": [
|
||||||
"parameters": [
|
{
|
||||||
{"name": "languages", "type": "array", "items": {"$ref": "LanguageCode"}, "description": "Array of LanguageCode"}
|
"name": "languages",
|
||||||
]
|
"type": "array",
|
||||||
}
|
"items": {
|
||||||
]
|
"$ref": "LanguageCode"
|
||||||
|
},
|
||||||
|
"description": "Array of LanguageCode"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "getMessage",
|
"name": "getMessage",
|
||||||
|
@ -85,44 +90,41 @@
|
||||||
"name": "text",
|
"name": "text",
|
||||||
"minimum": 0,
|
"minimum": 0,
|
||||||
"description": "User input string to be translated."
|
"description": "User input string to be translated."
|
||||||
},
|
}
|
||||||
{
|
],
|
||||||
"type": "function",
|
"returns_async": {
|
||||||
"name": "callback",
|
"name": "callback",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"name": "result",
|
"name": "result",
|
||||||
"description": "LanguageDetectionResult object that holds detected language reliability and array of DetectedLanguage",
|
"description": "LanguageDetectionResult object that holds detected langugae reliability and array of DetectedLanguage",
|
||||||
"properties": {
|
"properties": {
|
||||||
"isReliable": { "type": "boolean", "description": "CLD detected language reliability" },
|
"isReliable": {
|
||||||
"languages":
|
"type": "boolean",
|
||||||
{
|
"description": "CLD detected language reliability"
|
||||||
"type": "array",
|
},
|
||||||
"description": "array of detectedLanguage",
|
"languages": {
|
||||||
"items":
|
"type": "array",
|
||||||
{
|
"description": "array of detectedLanguage",
|
||||||
"type": "object",
|
"items": {
|
||||||
"description": "DetectedLanguage object that holds detected ISO language code and its percentage in the input string",
|
"type": "object",
|
||||||
"properties":
|
"description": "DetectedLanguage object that holds detected ISO language code and its percentage in the input string",
|
||||||
{
|
"properties": {
|
||||||
"language":
|
"language": {
|
||||||
{
|
"$ref": "LanguageCode"
|
||||||
"$ref": "LanguageCode"
|
},
|
||||||
},
|
"percentage": {
|
||||||
"percentage":
|
"type": "integer",
|
||||||
{
|
"description": "The percentage of the detected language"
|
||||||
"type": "integer",
|
}
|
||||||
"description": "The percentage of the detected language"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
}
|
||||||
}
|
]
|
||||||
]
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"events": []
|
"events": []
|
||||||
|
|
|
@ -208,7 +208,7 @@ describe('chrome extensions', () => {
|
||||||
};
|
};
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const customSession = session.fromPartition(`persist:${uuid.v4()}`);
|
const customSession = session.fromPartition(`persist:${uuid.v4()}`);
|
||||||
extension = await customSession.loadExtension(path.join(fixtures, 'extensions', 'chrome-i18n'));
|
extension = await customSession.loadExtension(path.join(fixtures, 'extensions', 'chrome-i18n', 'v2'));
|
||||||
w = new BrowserWindow({ show: false, webPreferences: { session: customSession, nodeIntegration: true, contextIsolation: false } });
|
w = new BrowserWindow({ show: false, webPreferences: { session: customSession, nodeIntegration: true, contextIsolation: false } });
|
||||||
await w.loadURL(url);
|
await w.loadURL(url);
|
||||||
});
|
});
|
||||||
|
@ -726,5 +726,95 @@ describe('chrome extensions', () => {
|
||||||
|
|
||||||
expect(message).to.equal('Hello from background.js');
|
expect(message).to.equal('Hello from background.js');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('chrome.i18n', () => {
|
||||||
|
let customSession: Session;
|
||||||
|
let w = null as unknown as BrowserWindow;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
customSession = session.fromPartition(`persist:${uuid.v4()}`);
|
||||||
|
await customSession.loadExtension(path.join(fixtures, 'extensions', 'chrome-i18n', 'v3'));
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
w = new BrowserWindow({
|
||||||
|
show: false,
|
||||||
|
webPreferences: {
|
||||||
|
session: customSession,
|
||||||
|
nodeIntegration: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(closeAllWindows);
|
||||||
|
|
||||||
|
it('getAcceptLanguages', async () => {
|
||||||
|
await w.loadURL(url);
|
||||||
|
|
||||||
|
const message = { method: 'getAcceptLanguages' };
|
||||||
|
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||||
|
|
||||||
|
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||||
|
const response = JSON.parse(responseString);
|
||||||
|
|
||||||
|
expect(response).to.be.an('array').that.is.not.empty('languages array is empty');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('getUILanguage', async () => {
|
||||||
|
await w.loadURL(url);
|
||||||
|
|
||||||
|
const message = { method: 'getUILanguage' };
|
||||||
|
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||||
|
|
||||||
|
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||||
|
const response = JSON.parse(responseString);
|
||||||
|
|
||||||
|
expect(response).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('getMessage', async () => {
|
||||||
|
await w.loadURL(url);
|
||||||
|
|
||||||
|
const message = { method: 'getMessage' };
|
||||||
|
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||||
|
|
||||||
|
const [, , responseString] = await once(w.webContents, 'console-message');
|
||||||
|
const response = JSON.parse(responseString);
|
||||||
|
|
||||||
|
expect(response).to.equal('Hola mundo!!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detectLanguage', async () => {
|
||||||
|
await w.loadURL(url);
|
||||||
|
|
||||||
|
const greetings = [
|
||||||
|
'Ich liebe dich', // German
|
||||||
|
'Mahal kita', // Filipino
|
||||||
|
'愛してます', // Japanese
|
||||||
|
'دوستت دارم', // Persian
|
||||||
|
'Minä rakastan sinua' // Finnish
|
||||||
|
];
|
||||||
|
const message = { method: 'detectLanguage', args: [greetings] };
|
||||||
|
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||||
|
|
||||||
|
const [, , responseString] = await once(w.webContents, 'console-message');
|
||||||
|
const response = JSON.parse(responseString);
|
||||||
|
|
||||||
|
expect(response).to.be.an('array');
|
||||||
|
|
||||||
|
for (const item of response) {
|
||||||
|
expect(Object.keys(item)).to.deep.equal(['isReliable', 'languages']);
|
||||||
|
}
|
||||||
|
|
||||||
|
const languages = response.map((r: { isReliable: boolean, languages: any[] }) => r.languages[0]);
|
||||||
|
expect(languages).to.deep.equal([
|
||||||
|
{ language: 'de', percentage: 100 },
|
||||||
|
{ language: 'fil', percentage: 100 },
|
||||||
|
{ language: 'ja', percentage: 100 },
|
||||||
|
{ language: 'ps', percentage: 100 },
|
||||||
|
{ language: 'fi', percentage: 100 }
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
6
spec/fixtures/extensions/chrome-i18n/v3/_locales/es/messages.json
vendored
Normal file
6
spec/fixtures/extensions/chrome-i18n/v3/_locales/es/messages.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"extName": {
|
||||||
|
"message": "Hola mundo!!",
|
||||||
|
"description": "Nombre de extensión"
|
||||||
|
}
|
||||||
|
}
|
36
spec/fixtures/extensions/chrome-i18n/v3/main.js
vendored
Normal file
36
spec/fixtures/extensions/chrome-i18n/v3/main.js
vendored
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/* global chrome */
|
||||||
|
|
||||||
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||||
|
sendResponse(request);
|
||||||
|
});
|
||||||
|
|
||||||
|
const map = {
|
||||||
|
getAcceptLanguages () {
|
||||||
|
chrome.i18n.getAcceptLanguages().then((languages) => {
|
||||||
|
console.log(JSON.stringify(languages));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
getMessage () {
|
||||||
|
const message = chrome.i18n.getMessage('extName');
|
||||||
|
console.log(JSON.stringify(message));
|
||||||
|
},
|
||||||
|
getUILanguage () {
|
||||||
|
const language = chrome.i18n.getUILanguage();
|
||||||
|
console.log(JSON.stringify(language));
|
||||||
|
},
|
||||||
|
async detectLanguage (texts) {
|
||||||
|
const result = [];
|
||||||
|
for (const text of texts) {
|
||||||
|
const language = await chrome.i18n.detectLanguage(text);
|
||||||
|
result.push(language);
|
||||||
|
}
|
||||||
|
console.log(JSON.stringify(result));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const dispatchTest = (event) => {
|
||||||
|
const { method, args = [] } = JSON.parse(event.data);
|
||||||
|
map[method](...args);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', dispatchTest, false);
|
17
spec/fixtures/extensions/chrome-i18n/v3/manifest.json
vendored
Normal file
17
spec/fixtures/extensions/chrome-i18n/v3/manifest.json
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "chrome-i18n",
|
||||||
|
"version": "1.0",
|
||||||
|
"default_locale": "es",
|
||||||
|
"content_scripts": [
|
||||||
|
{
|
||||||
|
"matches": [
|
||||||
|
"<all_urls>"
|
||||||
|
],
|
||||||
|
"js": [
|
||||||
|
"main.js"
|
||||||
|
],
|
||||||
|
"run_at": "document_start"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"manifest_version": 3
|
||||||
|
}
|
Loading…
Reference in a new issue