forked from FreeTubeApp/FreeTube
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
107 changed files
with
5,458 additions
and
7,484 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const { readFileSync, readdirSync } = require('fs') | ||
|
||
function getPreloadedLocales() { | ||
const localesFile = readFileSync(`${__dirname}/../node_modules/shaka-player/dist/locales.js`, 'utf-8') | ||
|
||
const localesLine = localesFile.match(/^\/\/ LOCALES: ([\w, -]+)$/m) | ||
|
||
if (!localesLine) { | ||
throw new Error("Failed to parse shaka-player's preloaded locales") | ||
} | ||
|
||
return localesLine[1].split(',').map(locale => locale.trim()) | ||
} | ||
|
||
function getAllLocales() { | ||
const filenames = readdirSync(`${__dirname}/../node_modules/shaka-player/ui/locales`) | ||
|
||
return new Set(filenames | ||
.filter(filename => filename !== 'source.json' && filename.endsWith('.json')) | ||
.map(filename => filename.replace('.json', ''))) | ||
} | ||
|
||
/** | ||
* Maps the shaka locales to FreeTube's active ones | ||
* This allows us to know which locale files are actually needed | ||
* and which shaka locale needs to be activated for a given FreeTube one. | ||
* @param {Set<string>} shakaLocales | ||
* @param {string[]} freeTubeLocales | ||
*/ | ||
function getMappings(shakaLocales, freeTubeLocales) { | ||
/** | ||
* @type {[string, string][]} | ||
* Using this structure as it gets passed to `new Map()` in the player component | ||
* The first element is the FreeTube locale, the second one is the shaka-player one | ||
**/ | ||
const mappings = [] | ||
|
||
for (const locale of freeTubeLocales) { | ||
if (shakaLocales.has(locale)) { | ||
mappings.push([ | ||
locale, | ||
locale | ||
]) | ||
} else if (shakaLocales.has(locale.replace('_', '-'))) { | ||
mappings.push([ | ||
locale, | ||
locale.replace('_', '-') | ||
]) | ||
} else if (shakaLocales.has(locale.split(/[-_]/)[0])) { | ||
mappings.push([ | ||
locale, | ||
locale.split(/[-_]/)[0] | ||
]) | ||
} | ||
} | ||
|
||
// special cases | ||
|
||
mappings.push( | ||
// according to https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | ||
// "no" is the macro language for "nb" and "nn" | ||
[ | ||
'nb_NO', | ||
'no' | ||
], | ||
[ | ||
'nn', | ||
'no' | ||
], | ||
|
||
// according to https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | ||
// "iw" is the old/original code for Hebrew, these days it's "he" | ||
[ | ||
'he', | ||
'iw' | ||
], | ||
|
||
// not sure why we have pt, pt-PT and pt-BR in the FreeTube locales | ||
// as pt and pt-PT are the same thing, but we should handle it here anyway | ||
[ | ||
'pt', | ||
'pt-PT' | ||
] | ||
) | ||
|
||
return mappings | ||
} | ||
|
||
function getShakaLocales() { | ||
const shakaLocales = getAllLocales() | ||
|
||
/** @type {string[]} */ | ||
const freeTubeLocales = JSON.parse(readFileSync(`${__dirname}/../static/locales/activeLocales.json`, 'utf-8')) | ||
|
||
const mappings = getMappings(shakaLocales, freeTubeLocales) | ||
|
||
const preloaded = getPreloadedLocales() | ||
|
||
const shakaMappings = mappings.map(mapping => mapping[1]) | ||
|
||
// use a set to deduplicate the list | ||
// we don't need to bundle any locale files that are already embedded in shaka-player/preloaded | ||
|
||
/** @type {string[]} */ | ||
const toBeBundled = [...new Set(shakaMappings.filter(locale => !preloaded.includes(locale)))] | ||
|
||
return { | ||
SHAKA_LOCALE_MAPPINGS: mappings, | ||
SHAKA_LOCALES_PREBUNDLED: preloaded, | ||
SHAKA_LOCALES_TO_BE_BUNDLED: toBeBundled | ||
} | ||
} | ||
|
||
module.exports = getShakaLocales() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// This script fixes shaka not exporting its type definitions and referencing remote google fonts in its CSS | ||
// by adding an export line to the type definitions and downloading the fonts and updating the CSS to point to the local files | ||
// this script only makes changes if they are needed, so running it multiple times doesn't cause any problems | ||
|
||
import { appendFileSync, closeSync, ftruncateSync, openSync, readFileSync, writeFileSync, writeSync } from 'fs' | ||
import { resolve } from 'path' | ||
|
||
const SHAKA_DIST_DIR = resolve(import.meta.dirname, '../node_modules/shaka-player/dist') | ||
|
||
function fixTypes() { | ||
let fixedTypes = false | ||
|
||
let fileHandleNormal | ||
try { | ||
fileHandleNormal = openSync(`${SHAKA_DIST_DIR}/shaka-player.ui.d.ts`, 'a+') | ||
|
||
const contents = readFileSync(fileHandleNormal, 'utf-8') | ||
|
||
// This script is run after every `yarn install`, even if shaka-player wasn't updated | ||
// So we want to check first, if we actually need to make any changes | ||
// or if the ones from the previous run are still intact | ||
if (!contents.includes('export default shaka')) { | ||
appendFileSync(fileHandleNormal, 'export default shaka;\n') | ||
|
||
fixedTypes = true | ||
} | ||
} finally { | ||
if (typeof fileHandleNormal !== 'undefined') { | ||
closeSync(fileHandleNormal) | ||
} | ||
} | ||
|
||
let fileHandleDebug | ||
try { | ||
fileHandleDebug = openSync(`${SHAKA_DIST_DIR}/shaka-player.ui.debug.d.ts`, 'a+') | ||
|
||
const contents = readFileSync(fileHandleDebug, 'utf-8') | ||
|
||
// This script is run after every `yarn install`, even if shaka-player wasn't updated | ||
// So we want to check first, if we actually need to make any changes | ||
// or if the ones from the previous run are still intact | ||
if (!contents.includes('export default shaka')) { | ||
appendFileSync(fileHandleDebug, 'export default shaka;\n') | ||
|
||
fixedTypes = true | ||
} | ||
} finally { | ||
if (typeof fileHandleDebug !== 'undefined') { | ||
closeSync(fileHandleDebug) | ||
} | ||
} | ||
|
||
if (fixedTypes) { | ||
console.log('Fixed shaka-player types') | ||
} | ||
} | ||
|
||
async function removeRobotoFont() { | ||
let cssFileHandle | ||
try { | ||
cssFileHandle = openSync(`${SHAKA_DIST_DIR}/controls.css`, 'r+') | ||
|
||
let cssContents = readFileSync(cssFileHandle, 'utf-8') | ||
|
||
const beforeReplacement = cssContents.length | ||
cssContents = cssContents.replace(/@font-face\{font-family:Roboto;[^}]+\}/, '') | ||
|
||
if (cssContents.length !== beforeReplacement) { | ||
ftruncateSync(cssFileHandle) | ||
writeSync(cssFileHandle, cssContents, 0, 'utf-8') | ||
|
||
console.log('Removed shaka-player Roboto font, so it uses ours') | ||
} | ||
} finally { | ||
if (typeof cssFileHandle !== 'undefined') { | ||
closeSync(cssFileHandle) | ||
} | ||
} | ||
} | ||
|
||
async function replaceAndDownloadMaterialIconsFont() { | ||
let cssFileHandle | ||
try { | ||
cssFileHandle = openSync(`${SHAKA_DIST_DIR}/controls.css`, 'r+') | ||
|
||
let cssContents = readFileSync(cssFileHandle, 'utf-8') | ||
|
||
const fontFaceRegex = /@font-face{font-family:'Material Icons Round'[^}]+format\('opentype'\)}/ | ||
|
||
if (fontFaceRegex.test(cssContents)) { | ||
const cssResponse = await fetch('https://fonts.googleapis.com/icon?family=Material+Icons+Round', { | ||
headers: { | ||
// Without the user-agent it returns the otf file instead of the woff2 one | ||
'user-agent': 'Firefox/125.0' | ||
} | ||
}) | ||
|
||
const text = await cssResponse.text() | ||
|
||
let newFontCSS = text.match(/(@font-face\s*{[^}]+})/)[1].replaceAll('\n', '') | ||
|
||
|
||
const urlMatch = newFontCSS.match(/https:\/\/fonts\.gstatic\.com\/s\/materialiconsround\/(?<version>[^\/]+)\/[^.]+\.(?<extension>[\w]+)/) | ||
|
||
const url = urlMatch[0] | ||
const { version, extension } = urlMatch.groups | ||
|
||
const fontResponse = await fetch(url) | ||
const fontContent = new Uint8Array(await fontResponse.arrayBuffer()) | ||
|
||
const filename = `shaka-materialiconsround-${version}.${extension}` | ||
writeFileSync(`${SHAKA_DIST_DIR}/${filename}`, fontContent) | ||
|
||
newFontCSS = newFontCSS.replace(url, `./${filename}`) | ||
|
||
cssContents = cssContents.replace(fontFaceRegex, newFontCSS) | ||
|
||
ftruncateSync(cssFileHandle) | ||
writeSync(cssFileHandle, cssContents, 0, 'utf-8') | ||
|
||
console.log('Changed shaka-player Material Icons Rounded font to use the smaller woff2 format instead of otf') | ||
console.log('Downloaded shaka-player Material Icons Rounded font') | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
} finally { | ||
if (typeof cssFileHandle !== 'undefined') { | ||
closeSync(cssFileHandle) | ||
} | ||
} | ||
} | ||
|
||
fixTypes() | ||
await removeRobotoFont() | ||
await replaceAndDownloadMaterialIconsFont() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.