Convert Global Google Search to MV3 (#680)
This commit is contained in:
Родитель
b717fe9ad3
Коммит
6db9c8d815
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// When you specify "type": "module" in the manifest background,
|
||||
// you can include the service worker as an ES Module,
|
||||
import { tldLocales } from './locales.js'
|
||||
|
||||
// Add a listener to create the initial context menu items,
|
||||
// context menu items only need to be created at runtime.onInstalled
|
||||
chrome.runtime.onInstalled.addListener(async () => {
|
||||
for (let [tld, locale] of Object.entries(tldLocales)) {
|
||||
chrome.contextMenus.create({
|
||||
id: tld,
|
||||
title: locale,
|
||||
type: 'normal',
|
||||
contexts: ['selection'],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Open a new search tab when the user clicks a context menu
|
||||
chrome.contextMenus.onClicked.addListener((item, tab) => {
|
||||
const tld = item.menuItemId
|
||||
let url = new URL(`https://google.${tld}/search`)
|
||||
url.searchParams.set('q', item.selectionText)
|
||||
chrome.tabs.create({ url: url.href, index: tab.index + 1 });
|
||||
});
|
||||
|
||||
// Add or removes the locale from context menu
|
||||
// when the user checks or unchecks the locale in the popup
|
||||
chrome.storage.onChanged.addListener(({ enabledTlds }) => {
|
||||
if (typeof enabledTlds === 'undefined') return
|
||||
|
||||
let allTlds = Object.keys(tldLocales)
|
||||
let currentTlds = new Set(enabledTlds.newValue);
|
||||
let oldTlds = new Set(enabledTlds.oldValue ?? allTlds);
|
||||
let changes = allTlds.map((tld) => ({
|
||||
tld,
|
||||
added: currentTlds.has(tld) && !oldTlds.has(tld),
|
||||
removed: !currentTlds.has(tld) && oldTlds.has(tld)
|
||||
}))
|
||||
|
||||
for (let { tld, added, removed } of changes) {
|
||||
if (added) {
|
||||
chrome.contextMenus.create({
|
||||
id: tld,
|
||||
title: tldLocales[tld],
|
||||
type: 'normal',
|
||||
contexts: ['selection'],
|
||||
});
|
||||
}
|
||||
else if (removed) {
|
||||
chrome.contextMenus.remove(tld);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 3.9 KiB |
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 584 B |
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 1.5 KiB |
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// TLD: top level domain; the "com" in "google.com"
|
||||
export const tldLocales = {
|
||||
'com.au': 'Australia',
|
||||
'com.br': 'Brazil',
|
||||
'ca': 'Canada',
|
||||
'cn': 'China',
|
||||
'fr': 'France',
|
||||
'it': 'Italy',
|
||||
'co.in': 'India',
|
||||
'co.jp': 'Japan',
|
||||
'com.ms': 'Mexico',
|
||||
'ru': 'Russia',
|
||||
'co.za': 'South Africa',
|
||||
'co.uk': 'United Kingdom'
|
||||
};
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "Global Google Search",
|
||||
"description": "Use the context menu to search a different country's Google",
|
||||
"version": "1.1",
|
||||
"manifest_version": 3,
|
||||
"permissions": ["contextMenus", "storage"],
|
||||
"background": {
|
||||
"service_worker": "background.js",
|
||||
"type": "module"
|
||||
},
|
||||
"action": {
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
"icons": {
|
||||
"16": "globalGoogle16.png",
|
||||
"48": "globalGoogle48.png",
|
||||
"128": "globalGoogle128.png"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Global Context Search</title>
|
||||
<style>
|
||||
body {
|
||||
min-width: 300px;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
input {
|
||||
margin: 5px;
|
||||
outline: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>Global Google Search</h2>
|
||||
<h3>Countries</h3>
|
||||
<form id="form"></form>
|
||||
<script src="popup.js" type="module"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -0,0 +1,50 @@
|
|||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// TLD: top level domain; the "com" in "google.com"
|
||||
import { tldLocales } from './locales.js'
|
||||
|
||||
createForm().catch(console.error);
|
||||
|
||||
async function createForm() {
|
||||
let { enabledTlds = Object.keys(tldLocales) } = await chrome.storage.sync.get('enabledTlds');
|
||||
let checked = new Set(enabledTlds)
|
||||
|
||||
let form = document.getElementById('form');
|
||||
for (let [tld, locale] of Object.entries(tldLocales)) {
|
||||
let checkbox = document.createElement('input');
|
||||
checkbox.type = 'checkbox';
|
||||
checkbox.checked = checked.has(tld);
|
||||
checkbox.name = tld;
|
||||
checkbox.addEventListener('click', (event) => {
|
||||
handleCheckboxClick(event).catch(console.error)
|
||||
})
|
||||
|
||||
let span = document.createElement('span');
|
||||
span.textContent = locale;
|
||||
|
||||
let div = document.createElement('div');
|
||||
div.appendChild(checkbox);
|
||||
div.appendChild(span);
|
||||
|
||||
form.appendChild(div);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCheckboxClick(event) {
|
||||
let checkbox = event.target
|
||||
let tld = checkbox.name
|
||||
let enabled = checkbox.checked
|
||||
|
||||
let { enabledTlds = Object.keys(tldLocales) } = await chrome.storage.sync.get('enabledTlds');
|
||||
let tldSet = new Set(enabledTlds)
|
||||
|
||||
if (enabled) tldSet.add(tld)
|
||||
else tldSet.delete(tld)
|
||||
|
||||
await chrome.storage.sync.set({ enabledTlds: [...tldSet] })
|
||||
}
|
||||
|
||||
|
||||
|
Загрузка…
Ссылка в новой задаче