mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-26 13:19:49 +07:00
* Update dictionary sample to use sidePanel.open() Updates the dictionary side panel example to use the sidePanel.open() API. This means we are also able to demonstrate a way of safely passing data to the side panel without hitting any race conditions. * Address feedback
50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
// Copyright 2023 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
const words = {
|
|
extensions:
|
|
'Extensions are software programs, built on web technologies (such as HTML, CSS, and JavaScript) that enable users to customize the Chrome browsing experience.',
|
|
popup:
|
|
"A UI surface which appears when an extension's action icon is clicked."
|
|
};
|
|
|
|
chrome.storage.session.get('lastWord', ({ lastWord }) => {
|
|
updateDefinition(lastWord);
|
|
});
|
|
|
|
chrome.storage.session.onChanged.addListener((changes) => {
|
|
const lastWordChange = changes['lastWord'];
|
|
|
|
if (!lastWordChange) {
|
|
return;
|
|
}
|
|
|
|
updateDefinition(lastWordChange.newValue);
|
|
});
|
|
|
|
function updateDefinition(word) {
|
|
// If the side panel was opened manually, rather than using the context menu,
|
|
// we might not have a word to show the definition for.
|
|
if (!word) return;
|
|
|
|
// Hide instructions.
|
|
document.body.querySelector('#select-a-word').style.display = 'none';
|
|
|
|
// Show word and definition.
|
|
document.body.querySelector('#definition-word').innerText = word;
|
|
document.body.querySelector('#definition-text').innerText =
|
|
words[word.toLowerCase()] ??
|
|
`Unknown word! Supported words: ${Object.keys(words).join(', ')}`;
|
|
}
|