Skip to content

Commit

Permalink
🔀 Merge pull request #410 from richardfrost/build_target_audio_sites
Browse files Browse the repository at this point in the history
Customize Web Audio Sites for Build Targets
  • Loading branch information
richardfrost authored May 18, 2022
2 parents bbeda89 + b626dd5 commit b72324b
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 261 deletions.
51 changes: 34 additions & 17 deletions bin/postbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ import {

let buildData;

function appleBuild(updateBuildNumber) {
const files = [
path.join('dist', 'img', 'donate.gif'),
path.join('dist', 'img', 'patreon-small.png'),
path.join('dist', 'img', 'patreon.png'),
];

removeOptionPageBookmarklet();
removeOptionPageDonations();
removeFiles(files);

if (buildData.release) {
updateSafariXcodeVersion(updateBuildNumber);
}
}

function common() {
handleManifestVersion();
handleVersion();
Expand Down Expand Up @@ -73,6 +89,11 @@ function handleVersion() {
}
}

function iOSBuild() {
const updateBuildNumber = false;
appleBuild(updateBuildNumber);
}

function main() {
buildData = loadJSONFile(buildFilePath);

Expand All @@ -83,6 +104,10 @@ function main() {
firefoxBuild();
}

if (buildData.target == 'ios') {
iOSBuild();
}

if (buildData.target == 'safari') {
safariBuild();
}
Expand Down Expand Up @@ -115,32 +140,24 @@ function removeOptionPageDonations() {
}

function safariBuild() {
const files = [
path.join('dist', 'img', 'donate.gif'),
path.join('dist', 'img', 'patreon-small.png'),
path.join('dist', 'img', 'patreon.png'),
];

removeOptionPageBookmarklet();
removeOptionPageDonations();
removeFiles(files);
if (buildData.release) {
updateSafariXcodeVersion();
}
const updateBuildNumber = true;
appleBuild(updateBuildNumber);
}

function updateSafariXcodeVersion() {
function updateSafariXcodeVersion(updateBuildNumber) {
const projectFilePath = path.join('safari', 'Advanced Profanity Filter.xcodeproj', 'project.pbxproj');
const projectFileText = fse.readFileSync(projectFilePath).toString();
let updatedProjectFileText = projectFileText.replace(/MARKETING_VERSION = \d+\.\d+\.\d+;$/gm, `MARKETING_VERSION = ${buildData.version};`);

// Increment the build version when running a Safari release
// macOS Apps require the build number to be larger than the previous build number
// https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleversion
const buildVersionMatch = projectFileText.match(/CURRENT_PROJECT_VERSION = (?<buildNumber>\d+);/);
if (buildVersionMatch.groups) {
const newBuildNumber = parseInt(buildVersionMatch.groups.buildNumber) + 1;
updatedProjectFileText = updatedProjectFileText.replace(/CURRENT_PROJECT_VERSION = (?<buildNumber>\d+);/gm, `CURRENT_PROJECT_VERSION = ${newBuildNumber};`);
if (updateBuildNumber) {
const buildVersionMatch = projectFileText.match(/CURRENT_PROJECT_VERSION = (?<buildNumber>\d+);/);
if (buildVersionMatch.groups) {
const newBuildNumber = parseInt(buildVersionMatch.groups.buildNumber) + 1;
updatedProjectFileText = updatedProjectFileText.replace(/CURRENT_PROJECT_VERSION = (?<buildNumber>\d+);/gm, `CURRENT_PROJECT_VERSION = ${newBuildNumber};`);
}
}

if (projectFileText !== updatedProjectFileText) {
Expand Down
9 changes: 9 additions & 0 deletions bin/prebuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function firefoxBuild() {
data.target = 'firefox';
}

function iOSBuild() {
data.target = 'ios';
data.config.muteMethod = 2; // Constants.MUTE_METHODS.VIDEO_MUTE;
}

function main() {
const argv = parseArgv(process.argv);
if (argv.count >= 2 && argv.count <= 4) {
Expand Down Expand Up @@ -66,6 +71,9 @@ function main() {
case '--firefox':
firefoxBuild();
break;
case '--ios':
iOSBuild();
break;
case '--manifestV2':
manifestV2Build();
break;
Expand Down Expand Up @@ -109,6 +117,7 @@ function targetFromData() {
return `--manifestV${data.manifestVersion}`;
case 'bookmarklet':
case 'firefox:':
case 'ios':
case 'safari':
return `--${data.target}`;
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"homepage": "https://github.com/richardfrost/AdvancedProfanityFilter#readme",
"scripts": {
"build:firefox": "node bin/prebuild.mjs --firefox && npm run build",
"build:ios": "node bin/prebuild.mjs --ios && npm run build",
"build:libs": "tsc -p ./src/script/lib/tsconfig.json",
"build:safari": "node bin/prebuild.mjs --safari && npm run build",
"build:static": "node bin/copy-static.mjs",
Expand All @@ -43,6 +44,7 @@
"release:all": "npm run release:mv2 && npm run release:mv3 && npm run release:firefox",
"release:build": "npm run build:static && webpack --config bin/webpack.config.js",
"release:firefox": "npm run package:firefox",
"release:ios": "npm run clean && node bin/prebuild.mjs --release --ios && npm run release:build",
"release:safari": "npm run clean && node bin/prebuild.mjs --release --safari && npm run release:build",
"release:mv2": "npm run package:mv2",
"release:mv3": "npm run package:mv3",
Expand Down
5 changes: 5 additions & 0 deletions src/script/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { upperCaseFirst } from './helper';
export default class Constants {
// Named Constants
static readonly ALL_WORDS_WORDLIST_ID = 0;
static readonly BUILD_TARGET_BOOKMARKLET = 'bookmarklet';
static readonly BUILD_TARGET_CHROME = 'chrome';
static readonly BUILD_TARGET_FIREFOX = 'firefox';
static readonly BUILD_TARGET_IOS = 'ios';
static readonly BUILD_TARGET_SAFARI = 'safari';
static readonly DOMAIN_MODES = { NORMAL: 0, ADVANCED: 1, DEEP: 2 };
static readonly FALSE = 0;
static readonly FILTER_METHODS = { CENSOR: 0, SUBSTITUTE: 1, REMOVE: 2, OFF: 3 };
Expand Down
10 changes: 10 additions & 0 deletions src/script/lib/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface AudioRule {
_dynamic?: boolean; // [Dynamic] Set to true on a dynamic rule
apfCaptions?: boolean; // [Cue] Display an HTML version of the caption/subtitle text: Requires videoCueHideCues = true
apfCaptionsSelector?: string; // [Cue] Selector for container that will hold the custom HTML captions
buildTarget?: string; // [All] Only allow rule to run on a specific buildTarget
checkInterval?: number; // [Watcher] Set a custom watch interval (in ms, Default: 20)
className?: string; // [Element] node.className.includes()
containsSelector?: string; // [Element] node.querySelector() [Not commonly used]
Expand Down Expand Up @@ -50,6 +51,10 @@ interface AudioRule {
videoSelector?: string; // [Cue,Watcher] Selector for video, also used for volume muteMethod (Default: 'video')
}

interface AudioSites {
[site: string]: AudioRule[];
}

interface BackgroundData {
disabledTab?: boolean;
}
Expand All @@ -60,6 +65,11 @@ interface BackgroundStorage {
};
}

interface BuildTargetSites {
disabledSites: string[];
sites: AudioSites;
}

interface ConfirmModalSettings {
backup?: boolean;
content?: string;
Expand Down
4 changes: 4 additions & 0 deletions src/script/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export function booleanToNumber(value: boolean): number {
return value ? Constants.TRUE : Constants.FALSE;
}

export function cloneWithJSON(object) {
return JSON.parse(JSON.stringify(object));
}

/* istanbul ignore next */
export function dynamicList(list: string[], select: HTMLSelectElement, upperCaseFirstChar: boolean = false, title?: string) {
removeChildren(select);
Expand Down
8 changes: 5 additions & 3 deletions src/script/optionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Domain from './domain';
import OptionAuth from './optionAuth';
import DataMigration from './dataMigration';
import Bookmarklet from './bookmarklet';
import WebAudioSites from './webAudioSites';
import WebAudio from './webAudio';
import Logger from './lib/logger';
import {
booleanToNumber,
Expand All @@ -26,6 +26,7 @@ export default class OptionPage {
darkModeButton: Element;
lightModeButton: Element;
prefersDarkScheme: boolean;
supportedAudioSites: AudioSites;
themeElements: Element[];

static readonly activeClass = 'w3-flat-belize-hole';
Expand Down Expand Up @@ -1711,7 +1712,7 @@ export default class OptionPage {
const select = document.querySelector('#supportedAudioSitesModal select#siteSelect') as HTMLSelectElement;
const textArea = document.querySelector('#supportedAudioSitesModal div#modalContentRight textarea') as HTMLTextAreaElement;
const config = {};
config[select.value] = WebAudioSites.sites[select.value];
config[select.value] = this.supportedAudioSites[select.value];
textArea.textContent = JSON.stringify(config, null, 2);
}

Expand All @@ -1722,7 +1723,8 @@ export default class OptionPage {
const select = contentLeft.querySelector('#siteSelect') as HTMLSelectElement;
removeChildren(select);

const sortedSites = Domain.sortedKeys(WebAudioSites.sites);
this.supportedAudioSites = WebAudio.supportedSites();
const sortedSites = Domain.sortedKeys(this.supportedAudioSites);
sortedSites.forEach((site) => {
const optionElement = document.createElement('option');
optionElement.value = site;
Expand Down
4 changes: 2 additions & 2 deletions src/script/popup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Constants from './lib/constants';
import { dynamicList } from './lib/helper';
import WebAudioSites from './webAudioSites';
import WebAudio from './webAudio';
import WebConfig from './webConfig';
import Domain from './domain';
import Page from './page';
Expand Down Expand Up @@ -137,7 +137,7 @@ class Popup {
dynamicList(wordlists, wordlistSelect);
wordlistSelect.selectedIndex = wordlistIndex;
if (this.cfg.muteAudio) {
this.audioSiteKeys = Object.keys(WebAudioSites.combineSites(this.cfg.customAudioSites));
this.audioSiteKeys = Object.keys(WebAudio.supportedAndCustomSites(this.cfg.customAudioSites));
if (this.audioSiteKeys.includes(this.domain.cfgKey)) {
audioPage = true;
const audioWordlistIndex = this.domain.audioWordlistId >= 0 ? this.domain.audioWordlistId + 1 : 0;
Expand Down
61 changes: 56 additions & 5 deletions src/script/webAudio.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Constants from './lib/constants';
import WebFilter from './webFilter';
import BookmarkletFilter from './bookmarkletFilter';
import WebAudioSites from './webAudioSites';
import { defaultTargetConfig, iOSTargetConfig, safariTargetConfig, supportedSites } from './webAudioSites';
import {
getElement,
getElements,
Expand Down Expand Up @@ -30,7 +30,7 @@ export default class WebAudio {
lastProcessedText: string;
muted: boolean;
rules: AudioRule[];
sites: { [site: string]: AudioRule[] };
sites: AudioSites;
siteKey: string;
supportedPage: boolean;
unmuteTimeout: number;
Expand Down Expand Up @@ -67,6 +67,53 @@ export default class WebAudio {
videoCueLanguage: 'language',
};

static getBuildTargetConfig() {
switch (WebConfig.BUILD.target) {
case Constants.BUILD_TARGET_IOS:
return iOSTargetConfig;
case Constants.BUILD_TARGET_SAFARI:
return safariTargetConfig;
default:
return defaultTargetConfig;
}
}

static removeUnsupportedSites(sites: AudioSites) {
Object.keys(sites).forEach((siteKey) => {
// Ensure site rules is an array
const siteRules = sites[siteKey];
if (!Array.isArray(siteRules)) {
sites[siteKey] = [siteRules];
}

// Remove any rules with a buildTarget that doesn't match
sites[siteKey] = sites[siteKey].filter((rule) => {
return rule.buildTarget == null || rule.buildTarget == WebConfig.BUILD.target;
});
});

// Remove sites without rules
Object.keys(sites).forEach((siteKey) => {
if (sites[siteKey].length == 0) {
delete sites[siteKey];
}
});
}

static supportedSites(removeUnsupported: boolean = true): AudioSites {
const buildTargetConfig = WebAudio.getBuildTargetConfig();
const siteConfig = Object.assign({}, supportedSites, buildTargetConfig.sites);
buildTargetConfig.disabledSites.forEach((disabledSite) => { delete siteConfig[disabledSite]; });
if (removeUnsupported) { WebAudio.removeUnsupportedSites(siteConfig); }
return siteConfig;
}

static supportedAndCustomSites(customConfig: AudioSites) {
const combinedSites = Object.assign({}, WebAudio.supportedSites(false), customConfig);
WebAudio.removeUnsupportedSites(combinedSites);
return combinedSites;
}

constructor(filter: WebFilter | BookmarkletFilter) {
this.filter = filter;
logger.setLevel(this.filter.cfg.loggingLevel);
Expand All @@ -84,7 +131,7 @@ export default class WebAudio {
) {
filter.cfg.customAudioSites = {};
}
this.sites = WebAudioSites.combineSites(filter.cfg.customAudioSites);
this.sites = WebAudio.supportedAndCustomSites(filter.cfg.customAudioSites);
this.volume = 1;
this.wordlistId = filter.audioWordlistId;
this.youTubeAutoSubsMax = filter.cfg.youTubeAutoSubsMax * 1000;
Expand All @@ -95,7 +142,6 @@ export default class WebAudio {
this.siteKey = this.getSiteKey();
this.rules = this.sites[this.siteKey];
if (this.rules) {
if (!Array.isArray(this.rules)) { this.rules = [this.rules]; }
this.rules.forEach((rule) => { this.initRule(rule); });
if (this.enabledRuleIds.length > 0) {
this.supportedPage = true;
Expand Down Expand Up @@ -448,6 +494,10 @@ export default class WebAudio {
this.initWatcherRule(rule);
if (!rule.disabled) { this.watcherRuleIds.push(ruleId); }
break;
case 'ytauto':
// This rule doesn't run like other rules, and is marked as disabled
rule.disabled = true;
break;
}
if (!rule.disabled) {
this.enabledRuleIds.push(ruleId);
Expand Down Expand Up @@ -488,7 +538,8 @@ export default class WebAudio {
this.filter.cfg.addWord(youTubeAutoCensor, youTubeAutoCensorOptions);

// Setup rule for YouTube Auto Subs
this.youTubeAutoSubsRule = { filterSubtitles: true, mode: 'ytauto', muteMethod: this.filter.cfg.muteMethod } as AudioRule;
this.youTubeAutoSubsRule = { mode: 'ytauto' } as AudioRule;
this.initRule(this.youTubeAutoSubsRule);
}
}

Expand Down
Loading

0 comments on commit b72324b

Please sign in to comment.