Skip to content

Commit

Permalink
support #48
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenYCL committed Jun 20, 2020
1 parent f74863e commit 889ec59
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-extension-udemy-translate",
"version": "2.0.8",
"version": "2.0.9",
"description": "Translate Udemy's subtitles into Chinese/English(netflix+udemy+lynda+hulu+hbo now字幕翻译)",
"license": "MIT",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"https://*.netflix.com/*",
"https://*.lynda.com/*",
"https://*.hbonow.com/*",
"https://*.hbomax.com/*",
"https://*.hulu.com/*",
"https://*.primevideo.com/*",
"https://*.amazon.com/*",
Expand All @@ -50,4 +51,4 @@
],
"manifest_version": 2,
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
}
110 changes: 110 additions & 0 deletions src/pages/Content/VideoType/hbomax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
hbo max version
*/

import { getItem } from '../modules/localStorage';
import { hiddenSubtitleCssInject } from '../modules/utils.ts';

const sub = {
pre: '',
current: '',
};

const getOriginText = () => {
let obj_text = '';
$('.class20.class30>span>span').forEach((span) => {
obj_text +=
' ' +
(span.innerText + ' ')
.replace('<br>', ' ')
.replace(/\[(.+)\]/, '')
.replace(/undefined/g, '')
.replace(/[\r\n]/g, '')
.trim();
});
return obj_text;
};

// sub.pre first time get
sub.pre = getOriginText();

const run = async () => {
let plugin_status = await getItem('status');
if (plugin_status) {
// cover css
hiddenSubtitleCssInject(['.default.class20.class30>span>span']);
let current = getOriginText();
// when change send request ,then make same
if (sub.pre !== current && current !== '') {
sub.pre = current;
console.log(sub);
// send message to background
if (typeof chrome.app.isInstalled !== 'undefined') {
chrome.runtime.sendMessage({ text: current });
}
}
} else {
// close plugin
await $('style[id=chrome-extension-plugin-css]').remove();
await $('.SUBTILTE').remove();
}
window.requestAnimationFrame(run);
};
run();

chrome.runtime.onMessage.addListener(async function(
request,
sender,
sendResponse
) {
console.log(JSON.stringify(request));
if (sub.current !== sub.pre) {
chrome.storage.sync.get(null, (items) => {
const subtitle = `<div class="SUBTILTE"
style="
position: absolute;
bottom:30px;
width:100%;
text-align: center;
margin: 0 .5em 1em;
padding: 20px 8px;
white-space: pre-line;
writing-mode: horizontal-tb;
unicode-bidi: plaintext;
direction: ltr;
-webkit-box-decoration-break: clone;
box-decoration-break: clone;
background: ${items['backgroundColor']};
opacity: ${items['backgroundColor']};
">
<div class="origin_subtitle"
style="
color:${items['origin_color']} !important;
font-weight:${items['origin_weight']} !important;
font-size:${items['origin_font']}px !important;;
"
>${request.origin}</div>
<div class="translate_subtitle"
style="
color: ${items['trans_color']} !important;
font-weight:${items['trans_weight']} !important;
font-size: ${items['trans_font']}px !important;
"
>${request.translate}</div>
</div>`;
let hasSubtitleDom = $('div.SUBTILTE').length === 0;
if (hasSubtitleDom) {
$('video')
.parent()
.parent()
.after(subtitle);
} else {
$('div.SUBTILTE').remove();
$('video')
.parent()
.parent()
.after(subtitle);
}
});
}
});
4 changes: 4 additions & 0 deletions src/pages/Content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const hrefMap = {
hulu: 'hulu',
amazon: 'amazon',
hbo: 'hbo',
hbomax:'hbomax',
primevideo: 'primevideo',
disneyplus: 'disneyplus',
};
Expand Down Expand Up @@ -44,6 +45,9 @@ switch (whatsPage(window.location.href)) {
case 'hbo':
require('./VideoType/hbo.ts');
break;
case 'hbomax':
require('./VideoType/hbomax.ts');
break;
case 'primevideo':
require('./VideoType/primevideo.ts');
break;
Expand Down

0 comments on commit 889ec59

Please sign in to comment.