Skip to content

Commit 2e2c778

Browse files
authored
Firefox users will now be prompted for permissions on the welcome page (#258)
1 parent db338e1 commit 2e2c778

File tree

11 files changed

+102
-16
lines changed

11 files changed

+102
-16
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# Changelog
22

3-
## v3.0.2
3+
## v3.1.0
44

55
<!--Releasenotes start-->
6-
- Fixed the fetch percentage in the shuffling page being misaligned.
6+
- Firefox: The welcome page now prompts users to allow the extension to access the youtube.com domain, this is needed for the extension to function.
7+
- Fixed a bug where the shuffle button in the popup would only work on the second try.
8+
- Firefox: Fixed the options page not being accessible.
9+
- Firefox: Fixed a bug where the extension was unable to retrieve the amount of local storage used.
710
<!--Releasenotes end-->
811

12+
## v3.0.2
13+
14+
- Fixed the fetch percentage in the shuffling page being misaligned.
15+
916
## v3.0.1
1017

1118
- Fixed the old "Ignore shorts" option not being updated to the new format correctly.

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ Do you have a favourite YouTube channel, but don't know what to watch? This exte
4949
The Random YouTube Video extension adds a 'Shuffle' button to YouTube channel, video and shorts pages, which will play a truly random video from the current channel. You can use the extension's popup to customize your experience further.
5050

5151
Highlighted Features:<br>
52+
- The shuffle button fits right in with the YouTube interface you're used to, for an optimal experience when browsing!
5253
- Choose from a wide range of options to individualize your experience, such as ignoring shorts, only shuffling from the most recent videos, shuffling multiple videos into a playlist, and much more!
53-
- You don't need to open YouTube to shuffle from your favourite channels - the extension popup allows you to shuffle from your most recently visited channel at any time!
54-
- If another user has already shuffled from the channel you want to watch, the extension will run faster for you as video IDs are shared!
54+
- Shuffle at any time by using the shuffle button in the extension popup, which allows you to shuffle from your most recently visited channel at any time!
55+
- Shuffles run even faster for you if another user has already shuffled from the channel you're watching, as video IDs are shared!
5556

5657
## Contribution
5758

@@ -89,6 +90,14 @@ Loading the extension like this will persist it until you remove it manually.
8990
Loading the extension like this will persist it only *until you restart Firefox*.
9091
You may also test the extension with Firefox by running `npm run dev:firefox`, which uses `web-ext` to load the extension in a temporary Firefox profile.
9192

93+
### Firefox for Android
94+
95+
- Make sure to have an Android device or Emulator set up for developer mode and running (follow [these instructions](https://extensionworkshop.com/documentation/develop/developing-extensions-for-firefox-for-android/#install-and-run-your-extension-in-firefox-for-android) to learn how to do so).
96+
- Run `adb devices` to get the device ID of your device or emulator.
97+
- Exchange the device ID in the `dev:android` script in `package.json` with the ID you just got. The default is `emulator-5554`.
98+
- Run `npm run dev:android` to load the extension in Firefox for Android.
99+
- Your device or emulator should now open Firefox for Android with the extension loaded.
100+
92101
---
93102

94103
If you enjoy this extension and want to say thanks, consider buying me a [coffee](https://ko-fi.com/nikkelm) or [sponsoring](https://github.com/sponsors/NikkelM) this project.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"name": "random-youtube-video",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "Customize, shuffle and play random videos from any YouTube channel.",
55
"scripts": {
66
"dev": "concurrently \"npm run dev:chromium\" \"npm run dev:firefox\"",
77
"dev:chromium": "webpack --env browser=chromium --watch --config webpack.dev.cjs",
88
"dev:firefox": "concurrently --kill-others \"webpack --env browser=firefox --watch --config webpack.dev.cjs\" \"web-ext run --source-dir ./dist/firefox\"",
9+
"dev:android": "concurrently --kill-others \"webpack --env browser=firefox --watch --config webpack.dev.cjs\" \"web-ext run --source-dir ./dist/firefox -t firefox-android --adb-device emulator-5554 --firefox-apk org.mozilla.fenix\"",
910
"build": "npm run build:chromium && npm run build:firefox",
1011
"build:chromium": "webpack --env browser=chromium --config webpack.prod.cjs",
1112
"build:firefox": "webpack --env browser=firefox --config webpack.prod.cjs",
1213
"lint": "eslint --ext .ts,.js --max-warnings=0 . --ignore-path .eslintignore",
14+
"lint:firefox": "web-ext lint --source-dir ./dist/firefox",
1315
"test": "c8 --reporter=lcov --reporter=text mocha ./test/testSetup.js ./test/chromeStorage.test.js ./test/**/*.test.js --require mocha-suppress-logs"
1416
},
1517
"type": "module",

src/background.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { configSync, setSyncStorageValue } from "./chromeStorage.js";
44

55
// ---------- Initialization/Chrome event listeners ----------
6+
const isFirefox = typeof browser !== "undefined";
67

78
// Check whether a new version was installed
89
async function initExtension() {
@@ -12,6 +13,10 @@ async function initExtension() {
1213
await setSyncStorageValue("previousVersion", manifestData.version);
1314
const welcomeUrl = chrome.runtime.getURL("html/welcome.html");
1415
await chrome.tabs.create({ url: welcomeUrl });
16+
} else if (isFirefox && !await browser.permissions.contains({ permissions: ["tabs"], origins: ["*://*.youtube.com/*"] })) {
17+
console.log("The extension is running in Firefox and does not have the required permissions.");
18+
const welcomeUrl = chrome.runtime.getURL("html/welcome.html");
19+
await chrome.tabs.create({ url: welcomeUrl });
1520
}
1621
// 3.0.0 introduced the previousVersion config value, so the update would not be handled correctly here
1722
if (configSync.previousVersion < manifestData.version || configSync.previousVersion === "3.0.0") {
@@ -25,8 +30,9 @@ await initExtension();
2530
// Make sure we are not using too much local storage
2631
async function checkLocalStorageCapacity() {
2732
// If over 90% of the storage quota for playlists is used, remove playlists that have not been accessed in a long time
28-
const utilizedStorage = await chrome.storage.local.getBytesInUse();
29-
const maxLocalStorage = chrome.storage.local.QUOTA_BYTES;
33+
const utilizedStorage = isFirefox ? JSON.stringify(await chrome.storage.local.get()).length : await chrome.storage.local.getBytesInUse();
34+
// Firefox does not offer a way to get the maximum local storage capacity, so we use 5MB as per the documentation
35+
const maxLocalStorage = isFirefox ? 5000000 : chrome.storage.local.QUOTA_BYTES;
3036

3137
console.log(`${((utilizedStorage / maxLocalStorage) * 100).toFixed(2)}% of local storage is used. (${utilizedStorage}/${maxLocalStorage} bytes)`);
3238

src/html/popup/popup.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ await setPopupDomElementValuesFromConfig(domElements);
3030
await setPopupDomElemenEventListeners(domElements);
3131
await determineOverlayVisibility(domElements);
3232

33+
// Restart the background script if it was stopped to make sure the shuffle button immediately works
34+
try {
35+
await chrome.runtime.sendMessage({ command: "connectionTest" });
36+
} catch (error) {
37+
console.log("The background worker was stopped and had to be restarted.");
38+
}
39+
3340
// ----- DOM -----
3441
// --- Private ---
3542
// Get relevant DOM elements

src/html/welcome.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
// Contains logic for the "Welcome" page
22
import { setSyncStorageValue } from "../chromeStorage.js";
33
import { buildShufflingHints, tryFocusingTab } from "./htmlUtils.js";
4+
import { delay } from "../utils.js";
45

56
// ----- Setup -----
7+
const isFirefox = typeof browser !== "undefined";
68
const domElements = getDomElements();
79

8-
// Show the "Reload all YouTube pages" div if there are youtube pages open
10+
let mayShowReloadAllYouTubePagesDiv = false;
911
chrome.tabs.query({}, function (tabs) {
1012
for (let i = 0; i <= tabs.length - 1; i++) {
1113
if (tabs[i].url.split("/")[2]?.includes("youtube")) {
12-
domElements.needToReloadYouTubePagesDiv.classList.remove("hidden");
14+
mayShowReloadAllYouTubePagesDiv = true;
15+
// Immediately show if we are not waiting for Firefox permissions
16+
if (!isFirefox) {
17+
domElements.needToReloadYouTubePagesDiv.classList.remove("hidden");
18+
}
1319
break;
1420
}
1521
}
@@ -29,6 +35,12 @@ function getDomElements() {
2935
// The document heading with the current version
3036
updateHeading: document.getElementById("updateHeading"),
3137

38+
// FIREFOX PERMISSIONS
39+
// The div containing the permission request button
40+
firefoxPermissionsDiv: document.getElementById("firefoxPermissionsDiv"),
41+
// The button to request permissions
42+
giveFirefoxPermissionsButton: document.getElementById("giveFirefoxPermissionsButton"),
43+
3244
// RELOAD YOUTUBE PAGES
3345
// The div containing the button and texts to reload all YouTube pages
3446
needToReloadYouTubePagesDiv: document.getElementById("needToReloadYouTubePagesDiv"),
@@ -53,6 +65,28 @@ function getDomElements() {
5365

5466
// Set event listeners for DOM elements
5567
async function setPopupDomElemenEventListeners(domElements) {
68+
// Firefox permissions button
69+
if (isFirefox && !await browser.permissions.contains({ permissions: ["tabs"], origins: ["*://*.youtube.com/*"] })) {
70+
domElements.firefoxPermissionsDiv.classList.remove("hidden");
71+
72+
// This is so important that we will use a browser alert window to make sure the user sees and acknowledges it
73+
await delay(50);
74+
alert("You need to grant the extension permission to run on YouTube in order to use it. Please grant permissions using the highlighted button.")
75+
76+
domElements.giveFirefoxPermissionsButton.addEventListener("click", async function () {
77+
await requestFirefoxPermissions();
78+
// If permissions were not granted we must ask again, without them the extension does not work
79+
if (!await browser.permissions.contains({ permissions: ["tabs"], origins: ["*://*.youtube.com/*"] })) {
80+
alert("You need to grant the extension permission to run on YouTube in order to use it. Please grant permissions.")
81+
} else {
82+
domElements.firefoxPermissionsDiv.classList.add("hidden");
83+
if (mayShowReloadAllYouTubePagesDiv) {
84+
domElements.needToReloadYouTubePagesDiv.classList.remove("hidden");
85+
}
86+
}
87+
});
88+
}
89+
5690
// Reload all YouTube pages button
5791
domElements.reloadAllYouTubePagesButton.addEventListener("click", async function () {
5892
let tabs = await chrome.tabs.query({});
@@ -87,4 +121,12 @@ async function setPopupDomElemenEventListeners(domElements) {
87121
await chrome.tabs.create({ url: changelogUrl });
88122
}
89123
});
124+
}
125+
126+
async function requestFirefoxPermissions() {
127+
const permissionsToRequest = {
128+
permissions: ["tabs"],
129+
origins: ["*://*.youtube.com/*"]
130+
}
131+
await browser.permissions.request(permissionsToRequest);
90132
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"background": {
33
"service_worker": "background.js"
4-
}
4+
},
5+
"options_page": "html/popup.html"
56
}

static/firefox-manifest-extra.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@
88
"gecko": {
99
"id": "RandomYouTubeVideo@NikkelM"
1010
}
11+
},
12+
"optional_permissions": [
13+
"tabs"
14+
],
15+
"options_ui": {
16+
"page": "html/popup.html"
1117
}
1218
}

static/html/welcome.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111
<div id="randomYoutubeVideo" style="margin-top: 10px">
1212
<h1 id="updateHeading">Random YouTube Video</h1>
1313
<h2>Welcome!</h2>
14-
15-
<br />
1614
<br />
15+
16+
<div id="firefoxPermissionsDiv" class="hidden">
17+
<br />
18+
<h3>To be able to use the extension, you <b>MUST</b></h3>
19+
<button id="giveFirefoxPermissionsButton" class="randomYoutubeVideoButton highlight-green" style="font-size: 20px">give the extension permission to access YouTube</button>
20+
</div>
21+
1722
<div id="needToReloadYouTubePagesDiv" class="hidden">
23+
<br />
24+
<br />
1825
<h3>Before you can start and see 'Shuffle' buttons, you need to</h3>
1926
<button id="reloadAllYouTubePagesButton" class="randomYoutubeVideoButton highlight-green" style="font-size: 20px">click me to reload all <i>YouTube.com</i> pages</button>
2027
<p>or do so manually.</p>

0 commit comments

Comments
 (0)