Table of contents
- Facts
- [Journal]
- Summary Mon 20 May: initial setup
- Summary Mon 28 May: initial setup
- Summary Sat 08 Jun: TIL button using button prototype
- [Journal]
- Commands used
- Resources
- For later
- Content scsripts: scripts that read and modify the content of a page. They make changes to their JavaScript environment without conflicting with their host page or other extensions' content scripts.
- "matches":
manifest.json/"matches"field can have one or more match patterns. These allow the browser to identify which sites to inject the content scripts into.
service worker: Extensions can monitor browser events in the background using the extension's service worker. Service workers are special JavaScript environments that handle events and terminate when they're not needed.- Extension service workers are an extension's central event handler.
- Extension service workers and web service workers are two different things.
- Like its web counterpart, an extension service worker ❌ cannot access the DOM, though you can use it if needed with offscreen documents.
- Synonyms:
background scripts,extension service worker - NOT synonyms:
web service worker
runtime.onInstalled(): The first event our service worker will listen for is runtime.onInstalled(). This method allows the extension to set an initial state or complete some tasks on installation. Extensions can use the Storage API and IndexedDB to store the application state._execute_actionkey runs the same code as the action.onClicked() event, so no additional code is needed.- Injecting scripts
scripting.executeScript()scripting.insertCSS()scripting.removeCSS()
- Badge: Browser actions can optionally display a badge—a bit of text that is layered over the icon. Badges make it easy to update the browser action to display a small amount of information about the state of the extension.
BadgebrowserAction.setBadgeTextbrowserAction.setBadgeBackgroundColor
- Extensions register their service worker in the manifest, which only takes a single JavaScript file. There's no need to call navigator.serviceWorker.register(), like you would in a web page.
- There are two methods of importing scripts into a service worker: the
importstatement and theimportScripts()method. Note that import(), often called a dynamic import, is not supported. - When the service worker has terminated, you will see
"service worker (inactive)"in thechrome://extensionspage. Click the "service worker (inactive)" link to inspect it. "permissions": ["storage"]: Chrome will shut down service workers if they are not needed. We use thechrome.storageAPI to persist state across service worker sessions. For storage access, request permission in the manifest.- Service workers don't have direct access to the window object and therefore cannot use
window.localStorageto store values. Also, service workers are short-lived execution environments; they get terminated repeatedly throughout a user's browser session, which makes them incompatible with global variables. Instead, usechrome.storage.localwhich stores data on the local machine. - All event listeners need to be statically registered in the global scope of the service worker.
- In other words, event listeners shouldn't be nested in async functions. This way Chrome can ensure that all event handlers are restored in case of a service worker reboot.
chrome.omnibox: The omnibox API allows you to register a keyword with Google Chrome's address bar, which is also known as the omnibox. When the user enters your extension's keyword, the user starts interacting solely with your extension. Each keystroke is sent to your extension, and you can provide suggestions in response. The suggestions can be richly formatted in a variety of ways. When the user accepts a suggestion, your extension is notified and can take action.onInputChanged()event: takes the current user input.suggestResultobject: is responsible for populating these suggestions.onInputEntered(): After the user selects a suggestion,onInputEntered()will open the corresponding Chrome API reference page.updateHistory(): takes the omnibox input and saves it tostorage.local. This way the most recent search term can be used later as an omnibox suggestion.setTimeout()orsetInterval()methods are commonly used to perform delayed or periodic tasks. However, these APIs can fail because the scheduler will cancel the timers when the service worker is terminated. Instead, extensions can use thechrome.alarms API.
"host_permission": use this to fetch data from a remote hosted location.
- The Action API controls the extension action (toolbar icon). When the user clicks on the extension action, it will either run some code or open a popup
chrome.permissionsAPI to request declared optional permissions at run time rather than install time, so users understand why the permissions are needed and grant only those that are necessary.tabs.query(): retrieve the tabs from specific URLs.tabs.update()windows.update()- JavaScript
Intl.Collator: this object enables language-sensitive string comparison.template tagURL constructor- Spread syntax
- Tabs
chrome.tabs: this API is used to group and ungroup tabs, or to query what tabs are in groups.chrome.tabGroups: this API allows you to interact with the browser's tab grouping system. Use this API to modify and rearrange tab groups in the browser.
- Element:
insertAdjacentElement()method https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement'beforebegin: Before the targetElement itself.afterbegin: Just inside the targetElement, before its first child.beforeend: Just inside the targetElement, after its last child.afterend: After the targetElement itself.
manifest.json: it is totally possible to run the extension locally on a local file that's not hosted on the internet. Make sure to give permissions to access and make changes to the target file.{ ... "content_scripts": [ { "css": ["styles.css"], "js": [ "scripts/buttonManager.js" ], "matches": [ "http://127.0.0.1:5500/*" ], "runAt": "document_start" } ], "permissions": [ "http://127.0.0.1:5500/*" ] }
html/css: Set up a mock web page with a target area for buttons (that will be created by extension).- Configured
manifest.json/"permissions"for local testing. JavaScript: Tested running a script on the target.
push()for arrayslet array = [1, 2, 3]; array.push(4);append()for DOM nodes (elements)let parent = document.getElementById('parent'); let newElement = document.createElement('div'); parent.append(newElement); // newElement is now a child of parent
- Tested DOM element structures for button composition
ul > [li, li, li ...]div > [button, button, button ...](adopted for now)
- Created functions for button set creation
- 21:30 Added function that shows/hides buttons on click.
- Added a textarea
TypeErrorReferenceError: this means the referenced variable is undefined.
varfor arrayslet array = [1, 2, 3]; array.push(4);letfor a block-scoped variable that might change (mutable).const: for block-scope + immutability- a block-scoped variable that should not be reassigned after its initial value is set.
- block-scope
const b = 60; b = 70; // TypeError: Assignment to constant variable. const obj = { key: 'value' }; obj.key = 'newValue'; // This is allowed console.log(obj.key); // 'newValue' - Immutability
{ const a = 50; console.log(a); // 50 } console.log(a); // ReferenceError: a is not defined
- Using the prototype buttons from the
102-prototype-buttonsbranch, created a new set of buttons. - Design and functions
- The buttons are the tree of all my markdown notes in my TIL repo.
- The default buttons represent the directories, and clicking on the dropdown buttons will open a new tab of each markdown notes.
css: the styling follows the Gitlab's css styles.
Install code command to VS Code
shift + command + p > Shell Command: Install 'code' command in PATH
Invoke emojis
control + command + space
-
Create
manifest.jsonhttps://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world -
Structure your extension project https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#structure
git tag https://minsone.github.io/git/git-addtion-and-modified-delete-tag
- Extension service worker https://developer.chrome.com/docs/extensions/develop/concepts/service-workers
- Scripting API
chrome.scriptinghttps://developer.chrome.com/docs/extensions/reference/api/scripting - Badge https://developer.chrome.com/docs/extensions/mv2/reference/browserAction#badge
- Service worker tutorial https://developer.chrome.com/docs/extensions/get-started/tutorial/service-worker-events
- Import scripts into service workers https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/basics#import-scripts
- Debugging the service worker https://developer.chrome.com/docs/extensions/get-started/tutorial/service-worker-events#step-3
Intl.Collatorhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator
- Stop button icons https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/tutorial.reading-time/images
- Turn string into code
<code translate="no" dir="ltr">TEXT HERE</code>
- The developer's guide has dozens of additional links to pieces of documentation relevant to advanced extension creation.
- Extensions have access to powerful APIs beyond what's available on the open web. The Chrome APIs documentation walks through each API.
scripting.executeScript()https://developer.chrome.com/docs/extensions/reference/api/scripting#injected-code- The
"activeTab"permission https://developer.chrome.com/docs/extensions/develop/concepts/activeTab#what-activeTab-allows
- Debugging extensions https://developer.chrome.com/docs/extensions/get-started/tutorial/debug#debug_bg
- The extension service worker lifecycle https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle#persist-data
- Persist data rather than using global variables https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle#persist-data
chrome.omniboxAPI https://developer.chrome.com/docs/extensions/reference/api/omniboxonInputChanged()event https://developer.chrome.com/docs/extensions/reference/api/omnibox#event-onInputChangedSuggestResultobject https://developer.chrome.com/docs/extensions/reference/api/omnibox#type-SuggestResult
- ⭐️ Message passing https://developer.chrome.com/docs/extensions/develop/concepts/messaging
- Popover API https://developer.mozilla.org/en-US/docs/Web/API/Popover_API
chrome.permissionshttps://developer.chrome.com/docs/extensions/reference/api/permissionschrome.tabGroupshttps://developer.chrome.com/docs/extensions/reference/api/tabGroups


