Skip to content

Commit 6116518

Browse files
authored
Add mole game tutorial (#1433)
LGTN
1 parent 8cf0e9a commit 6116518

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

Diff for: functional-samples/tutorial.mole-game/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Mole Game
2+
3+
This mole game is based on a YouTube tutorial in our Chrome Extensions series. Watch it [here](https://goo.gle/Chrome-Ext).
4+
5+
<img src="mole-game.png" width=500>
6+
7+
## Overview
8+
9+
In the sample, moles periodically appear from pipes in the browser toolbar. You score points by clicking the icon before the mole disappears.
10+
11+
If enabled, a browser tab is closed if you miss one of the moles.
12+
13+
## Implementation Notes
14+
15+
Each icon in the browser toolbar is a seperate extension. The extensions communicate using the `chrome.runtime.sendMessage` API and the `chrome.runtime.onMessageExternal` event.
16+
17+
To discover mole extensions, the controller extension uses the `chrome.management` API.
18+
19+
By default, the tab closing behavior is disabled. You can enable this by commenting out the line in `mole/service-worker.js`.
20+
21+
## Running this extension
22+
23+
1. Clone this repository.
24+
1. Make several copies of the `mole` directory.
25+
1. Load the `controller` directory and all mole directories in Chrome as [unpacked extensions](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
26+
1. Wait for a mole to appear!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "mole controller",
3+
"version": "1.0",
4+
"manifest_version": 3,
5+
"background": {
6+
"service_worker": "service-worker.js"
7+
},
8+
"permissions": ["management", "alarms"],
9+
"action": {}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
chrome.runtime.onInstalled.addListener(() => {
2+
chrome.alarms.create({ periodInMinutes: (1 / 60) * 3 });
3+
});
4+
5+
chrome.alarms.onAlarm.addListener(async () => {
6+
const extensions = await chrome.management.getAll();
7+
const moles = extensions.filter((e) => e.name === 'mole');
8+
9+
const randomIndex = Math.floor(Math.random() * moles.length);
10+
11+
chrome.runtime.sendMessage(moles[randomIndex].id, { id: chrome.runtime.id });
12+
});
13+
14+
let counter = 0;
15+
16+
chrome.runtime.onMessageExternal.addListener(() => {
17+
counter++;
18+
chrome.action.setBadgeText({ text: `${counter}` });
19+
});

Diff for: functional-samples/tutorial.mole-game/mole-game.png

386 KB
Loading
7.78 KB
Loading
12.4 KB
Loading
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "mole",
3+
"version": "1.0",
4+
"manifest_version": 3,
5+
"background": {
6+
"service_worker": "service-worker.js"
7+
},
8+
"action": {
9+
"default_icon": "icon-empty.png"
10+
},
11+
"permissions": ["management", "tabs"]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
let failTimeout;
2+
let moleShowing = false;
3+
let controllerId;
4+
5+
chrome.action.onClicked.addListener(() => {
6+
if (!moleShowing) return;
7+
8+
chrome.runtime.sendMessage(controllerId, 'success');
9+
hideMole();
10+
11+
failTimeout && clearTimeout(failTimeout);
12+
failTimeout = undefined;
13+
});
14+
15+
function showMole() {
16+
chrome.action.setIcon({ path: 'icon-mole.png' });
17+
moleShowing = true;
18+
}
19+
20+
function hideMole() {
21+
chrome.action.setIcon({ path: 'icon-empty.png' });
22+
moleShowing = false;
23+
}
24+
25+
chrome.runtime.onMessageExternal.addListener((msg) => {
26+
controllerId = msg.id;
27+
showMole();
28+
failTimeout = setTimeout(async () => {
29+
hideMole();
30+
const tabs = await chrome.tabs.query({});
31+
const eligibleTabs = tabs.filter((t) => t.title.includes('Example'));
32+
33+
if (eligibleTabs.length > 0) {
34+
// const tabToClose = Math.floor(Math.random() * eligibleTabs.length);
35+
// chrome.tabs.remove(eligibleTabs[tabToClose].id);
36+
}
37+
}, 2000);
38+
});

0 commit comments

Comments
 (0)