|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +chrome.runtime.onInstalled.addListener(() => { |
| 16 | + chrome.action.setBadgeText({ |
| 17 | + text: 'OFF' |
| 18 | + }); |
| 19 | +}); |
| 20 | + |
| 21 | +const extensions = 'https://developer.chrome.com/docs/extensions'; |
| 22 | +const webstore = 'https://developer.chrome.com/docs/webstore'; |
| 23 | + |
| 24 | +// When the user clicks on the extension action |
| 25 | +chrome.action.onClicked.addListener(async (tab) => { |
| 26 | + if (tab.url?.startsWith(extensions) || tab.url?.startsWith(webstore)) { |
| 27 | + // We retrieve the action badge to check if the extension is 'ON' or 'OFF' |
| 28 | + const prevState = await chrome.action.getBadgeText({ tabId: tab.id }); |
| 29 | + // Next state will always be the opposite |
| 30 | + const nextState = prevState === 'ON' ? 'OFF' : 'ON'; |
| 31 | + |
| 32 | + // Set the action badge to the next state |
| 33 | + await chrome.action.setBadgeText({ |
| 34 | + tabId: tab.id, |
| 35 | + text: nextState |
| 36 | + }); |
| 37 | + |
| 38 | + if (nextState === 'ON') { |
| 39 | + chrome.tabs.sendMessage(tab.id, { data: 'focus-on' }); |
| 40 | + } else if (nextState === 'OFF') { |
| 41 | + chrome.tabs.sendMessage(tab.id, { data: 'focus-off' }); |
| 42 | + } |
| 43 | + } |
| 44 | +}); |
0 commit comments