Skip to content

Commit 6ca774c

Browse files
author
Simeon Vincent
authored
Adds simple Hello World example (GoogleChrome#553)
1 parent 40e5a5c commit 6ca774c

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The directory structure is as follows:
1414
* [api/](api/) - extensions focused on a single API package
1515
* (To be added) [howto/](howto/) - extensions that show how to perform a particular task
1616
* [tutorials/](tutorials/) - multi-step walkthroughs referenced inline in the docs
17-
* (To be added) [extensions/](extensions/) - full featured extensions spanning multiple API packages
17+
* [extensions/](extensions/) - full featured extensions spanning multiple API packages
1818
* [apps/](apps/) - deprecated Chrome Apps platform (not listed below)
1919
* [mv2-archive/](mv2-archive/) - resources for manifest version 2
2020

@@ -29,6 +29,19 @@ Read more on [Getting Started](https://developer.chrome.com/extensions/getstarte
2929
</tr>
3030
</thead>
3131
<tbody>
32+
<tr>
33+
<td style="vertical-align:top;">
34+
Hello World <br>
35+
<a href="examples/hello-world"><code>examples/hello-world</code></a>
36+
</td>
37+
<td style="vertical-align:top;">
38+
<ul>
39+
<li><a href="https://developer.chrome.com/docs/extensions/reference/action/#event-onClicked">action.onClicked</a></li>
40+
<li><a href="https://developer.chrome.com/docs/extensions/reference/runtime/#method-getURL">runtime.getURL</a></li>
41+
<li><a href="https://developer.chrome.com/docs/extensions/reference/tabs/#method-create">tabs.create</a></li>
42+
</ul>
43+
</td>
44+
</tr>
3245
<tr>
3346
<td style="vertical-align:top;">
3447
Cookie Clearer <br>

examples/hello-world/background.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Extension event listeners are a little different from the patterns you may have seen in DOM or
2+
// Node.js APIs. The below event listener registration can be broken in to 4 distinct parts:
3+
//
4+
// * chrome - the global namespace for Chrome's extension APIs
5+
// * runtime – the namespace of the specific API we want to use
6+
// * onInstalled - the event we want to subscribe to
7+
// * addListener - what we want to do with this event
8+
//
9+
// See https://developer.chrome.com/docs/extensions/reference/events/ for additional details.
10+
chrome.runtime.onInstalled.addListener(async () => {
11+
12+
// While we could have used `let url = "hello.html"`, using runtime.getURL is a bit more robust as
13+
// it returns a full URL rather than just a path that Chrome needs to be resolved contextually at
14+
// runtime.
15+
let url = chrome.runtime.getURL("hello.html");
16+
17+
// Open a new tab pointing at our page's URL using JavaScript's object initializer shorthand.
18+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#new_notations_in_ecmascript_2015
19+
//
20+
// Many of the extension platform's APIs are asynchronous and can either take a callback argument
21+
// or return a promise. Since we're inside an async function, we can await the resolution of the
22+
// promise returned by the tabs.create call. See the following link for more info on async/await.
23+
// https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
24+
let tab = await chrome.tabs.create({ url });
25+
26+
// Finally, let's log the ID of the newly created tab using a template literal.
27+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
28+
//
29+
// To view this log message, open chrome://extensions, find "Hello, World!", and click the
30+
// "service worker" link in th card to open DevTools.
31+
console.log(`Created tab ${tab.id}`);
32+
});

examples/hello-world/hello.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello, World!</title>
5+
</head>
6+
<body>
7+
<p>Hello, World!</p>
8+
</body>
9+
</html>

examples/hello-world/manifest.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Hello, World!",
3+
"version": "1.0",
4+
"manifest_version": 3,
5+
"background": {
6+
"service_worker": "background.js"
7+
},
8+
"action": {}
9+
}

0 commit comments

Comments
 (0)