Skip to content

Commit

Permalink
Custom source pattern support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrews54757 committed Dec 18, 2023
1 parent 350471f commit 6500ee7
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
6 changes: 6 additions & 0 deletions chrome/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,12 @@
"options_autourl_hint": {
"message": "Hint: Test regex on"
},
"options_pattern_header": {
"message": "Custom Source Patterns"
},
"options_pattern_body": {
"message": "These patterns are used to detect custom video sources on webpages. They are applied in order, and the first match is used. For advanced users only."
},
"options_help_header": {
"message": "Help"
},
Expand Down
6 changes: 6 additions & 0 deletions chrome/_locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,12 @@
"options_autourl_hint": {
"message": "Sugerencia: Prueba regex en"
},
"options_pattern_header": {
"message": "Patrones de Fuente Personalizados"
},
"options_pattern_body": {
"message": "Estos patrones se utilizan para detectar fuentes de video personalizadas en páginas web. Se aplican en orden y se usa la primera coincidencia. Solo para usuarios avanzados."
},
"options_help_header": {
"message": "Ayuda"
},
Expand Down
6 changes: 6 additions & 0 deletions chrome/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,12 @@
"options_autourl_hint": {
"message": "ヒント: 正規表現のテストができます"
},
"options_pattern_header": {
"message": "カスタムソースパターン"
},
"options_pattern_body": {
"message": "これらのパターンは、Web ページ上のカスタム動画ソースを検出するために使用されます。順番に適用され、最初に一致したものが使用されます。上級者向け。"
},
"options_help_header": {
"message": "ヘルプ"
},
Expand Down
46 changes: 40 additions & 6 deletions chrome/background/background.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {StreamSaverBackend} from './StreamSaverBackend.mjs';
let Options = {};

const AutoEnableList = [];
const CustomSourcePatterns = [];
const ExtensionVersion = chrome.runtime.getManifest().version;
const Logging = false;
const PlayerURL = chrome.runtime.getURL('player/index.html');
Expand Down Expand Up @@ -425,11 +426,37 @@ async function loadOptions(newOptions) {
}

if (urlStr[0] === '~') {
AutoEnableList.push(new RegExp(urlStr.substring(1)));
try {
AutoEnableList.push(new RegExp(urlStr.substring(1)));
} catch (e) {
}
} else {
AutoEnableList.push(urlStr);
}
});

CustomSourcePatterns.length = 0;
Options.customSourcePatterns.split('\n') .forEach((line)=>{
line = line.trim();
if (line.length === 0) return;
if (line.startsWith('#') || line.startsWith('//')) return;

const args = line.split(' ');
const extStr = args[0];
const regexStr = args.slice(1).join(' ');

// parse regex and flags
const regex = regexStr.substring(1, regexStr.lastIndexOf('/'));
const flags = regexStr.substring(regexStr.lastIndexOf('/') + 1);
try {
CustomSourcePatterns.push({
regex: new RegExp(regex, flags),
ext: extStr,
});
} catch (e) {

}
});
}

async function setupRedirectRule(ruleID, filetypes) {
Expand Down Expand Up @@ -762,10 +789,21 @@ function frameHasPlayer(frame) {
chrome.webRequest.onHeadersReceived.addListener(
(details) => {
const url = details.url;
const ext = URLUtils.get_url_extension(url);
let ext = URLUtils.get_url_extension(url);
const frame = getOrCreateFrame(details);
if (frameHasPlayer(frame)) return;

if ((details.statusCode >= 400 && details.statusCode < 600) || details.statusCode === 204) {
return; // Client or server error. Ignore it
}

const customSourcePattern = CustomSourcePatterns.find((item)=>{
return item.regex.test(url);
});
if (customSourcePattern) {
ext = customSourcePattern.ext;
}

if (isSubtitles(ext)) {
return handleSubtitles(url, frame, frame.requestHeaders[details.requestId]);
}
Expand All @@ -778,10 +816,6 @@ chrome.webRequest.onHeadersReceived.addListener(
}
}

if ((details.statusCode >= 400 && details.statusCode < 600) || details.statusCode === 204) {
return; // Client or server error. Ignore it
}

onSourceRecieved(details, frame, mode);
}, {
urls: ['<all_urls>'],
Expand Down
1 change: 1 addition & 0 deletions chrome/player/options/defaults/DefaultOptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const DefaultOptions = {
autoEnableBestSubtitles: false,
autoplayYoutube: true,
autoEnableURLs: [],
customSourcePatterns: ``,
keybinds: DefaultKeybinds,
videoBrightness: 1,
videoContrast: 1,
Expand Down
4 changes: 4 additions & 0 deletions chrome/player/options/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ <h1 data-i18n="options_autourl_header"></h1>
<p><span data-i18n="options_autourl_hint"></span> <a target="_blank" rel="noopener noreferrer" href="https://regex101.com/">Regex101</a></p>
<textarea id="autoEnableURLs" data-i18n-label="options_autourl_body"></textarea>

<h1 data-i18n="options_pattern_header"></h1>
<p data-i18n="options_pattern_body"></p>
<textarea id="customSourcePatterns" data-i18n-label="options_pattern_body"></textarea>

<h1 data-i18n="options_export_header"></h1>

<button id="import" data-i18n="options_import"></button>
Expand Down
6 changes: 4 additions & 2 deletions chrome/player/options/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ p.hint {
color: rgb(0, 0, 255);
}

#autoEnableURLs {
#autoEnableURLs,
#customSourcePatterns {
color: black;
background-color: transparent;
font-size: 12px;
Expand All @@ -124,7 +125,8 @@ p.hint {
resize: none;
}

#autoEnableURLs:focus {
#autoEnableURLs:focus,
#customSourcePatterns:focus {
border: 1px solid blue;
}

Expand Down
7 changes: 7 additions & 0 deletions chrome/player/options/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const exportButton = document.getElementById('export');
const clickAction = document.getElementById('clickaction');
const dblclickAction = document.getElementById('dblclickaction');
const tplclickAction = document.getElementById('tplclickaction');
const customSourcePatterns = document.getElementById('customSourcePatterns');
autoEnableURLSInput.setAttribute('autocapitalize', 'off');
autoEnableURLSInput.setAttribute('autocomplete', 'off');
autoEnableURLSInput.setAttribute('autocorrect', 'off');
Expand Down Expand Up @@ -60,6 +61,7 @@ async function loadOptions(newOptions) {
seekStepSize.value = Math.round(Options.seekStepSize * 100) / 100;
playbackRate.value = Options.playbackRate;
qualityMultiplier.value = Options.qualityMultiplier;
customSourcePatterns.value = Options.customSourcePatterns || '';

setSelectMenuValue(clickAction, Options.singleClickAction);
setSelectMenuValue(dblclickAction, Options.doubleClickAction);
Expand Down Expand Up @@ -296,6 +298,11 @@ autoEnableURLSInput.addEventListener('input', (e) => {
optionChanged();
});

customSourcePatterns.addEventListener('input', (e) => {
Options.customSourcePatterns = customSourcePatterns.value;
optionChanged();
});

importButton.addEventListener('click', () => {
const picker = document.createElement('input');
picker.type = 'file';
Expand Down

0 comments on commit 6500ee7

Please sign in to comment.