Skip to content

Commit

Permalink
Changing the block display method
Browse files Browse the repository at this point in the history
  • Loading branch information
xjl0 committed Nov 4, 2022
1 parent 5ac7402 commit c031973
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions src/chromeServices/anime365_content.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
export {}
let timerId: string | number | NodeJS.Timeout | undefined;
let countNextPage: number = 0;
let countTimerOut: number = 0;
let countCurrentTime: number = 0;
let countDurationTime: number = 0;
let styleElem: HTMLStyleElement | undefined;
let hrefChevronRight: string;
chrome.runtime.onMessage.addListener(function (request) {
// Получаем сообщение от background что страница готова
if (request && request.type === 'page-rendered-anime365') {
//Очистим таймер если есть
clearInterval(timerId);
//Обнулим таймер перехода
countTimerOut = 0;
//Проверим наличие плеера
if (document.getElementById("videoFrame")) {
const videoFrame = document.getElementById("videoFrame")!;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const iFrame = (<HTMLIFrameElement>videoFrame).contentWindow;
//Пределим наличие кнопки на следующую серию
if (document.getElementsByClassName("m-select-sibling-episode").item(0)?.getElementsByTagName("i").item(1)){
hrefChevronRight = document.getElementsByClassName("m-select-sibling-episode").item(0)?.getElementsByTagName("a").item(1)?.href || "/";
}else if(document.getElementsByClassName("m-select-sibling-episode").item(0)?.getElementsByTagName("i").item(0)?.textContent === "chevron_right"){
hrefChevronRight = document.getElementsByClassName("m-select-sibling-episode").item(0)?.getElementsByTagName("a").item(0)?.href || "/";
}else{
hrefChevronRight = "/";
}
//Запустим таймер
timerId = setTimeout(function tick() {
//Получим состояние полоски загрузки
const str = iFrame!.document.getElementsByClassName("vjs-play-progress").item(0)!.getAttribute("style")!.trim();
countNextPage = parseInt(str.replace(/^width: ([0-9]{1,2})(.{0,1})?([0-9]{0,2})?%;$/, "$1"));
//Если просмотренно более 96% тайтла запускаем отображение пропуска
if (videoFrame && countNextPage >= 96) {
//Получим состояние видео
countCurrentTime = iFrame?.document.getElementsByTagName("video").item(0)?.currentTime || 0;
countDurationTime = iFrame?.document.getElementsByTagName("video").item(0)?.duration || 0;
//Если до конца тайтла осталось 15 секунд покажем блок
if (videoFrame && countDurationTime - 15 <= countCurrentTime && countDurationTime > 0 && countCurrentTime > 0 && hrefChevronRight !== "/") {
//Вернём наличие нашего блока
const mainBlock = iFrame!.document.getElementById("answer_is_forty_two");
const mainBlock = iFrame?.document.getElementById("answer_is_forty_two");
//Если блока нет
if (!mainBlock) {
//Созданим элементы блока
Expand All @@ -34,38 +43,36 @@ chrome.runtime.onMessage.addListener(function (request) {
a.onclick = function () {
//При нажатии на блок очистим таймер и удалим блок
clearInterval(timerId);
iFrame!.document.getElementById("answer_is_forty_two")?.remove();
countTimerOut = 0;
iFrame?.document.getElementById("answer_is_forty_two")?.remove();
styleElem?.remove();
}
div.appendChild(a).appendChild(span);
const styleElem = iFrame!.document.head.appendChild(document.createElement("style"));
styleElem.innerHTML = "#answer_is_forty_two{background-color: #ff6d0033; border-radius: 2px; position:absolute; bottom:70px; right:20px; transform: translate(-50%, -50%);}" +
"#answer_is_forty_two a{display: block; cursor: pointer; width: 200px; height: 40px; line-height: 40px; font-size: 18px; font-family: sans-serif; text-decoration: none; color: #333;border: 1px solid #333;letter-spacing: 2px;text-align: center;position: relative;transition: all .35s;}" +
"#answer_is_forty_two a:after{position: absolute; content: \"\"; top: 0; left: 0; width: var(--width, 0); height: 100%; background: #ff6d00; transition: all .35s;}" +
styleElem = iFrame?.document.head.appendChild(document.createElement("style"));
styleElem!.innerHTML = "#answer_is_forty_two{background-color: #ff6d0033; position:absolute; bottom:10%; right:5%;}" +
"#answer_is_forty_two a{border-radius: 2px; display: block; cursor: pointer; width: 200px; height: 40px; line-height: 40px; font-size: 18px; font-family: sans-serif; text-decoration: none; color: #fff;border: 1px solid #fff;letter-spacing: 2px;text-align: center;position: relative;transition: all .35s;}" +
"#answer_is_forty_two a:after{border-radius: 2px; position: absolute; content: \"\"; top: 0; left: 0; width: var(--width, 0); height: 100%; background: #ff6d00; transition: all .35s;}" +
"#answer_is_forty_two a:hover{color: #fff; background: #ff6d0088;}" +
"#answer_is_forty_two a span{position: relative; z-index: 2;}";
//Добавим блок во фрейм
const container = iFrame!.document.getElementsByClassName("vjs-control-bar").item(0)!;
const container = iFrame?.document.getElementsByClassName("vjs-control-bar").item(0)!;
const parent = container.parentNode!
parent.insertBefore(div, container)
} else {
//Считаем 25 секунд и переходим по ссылке
countTimerOut++;
if (countTimerOut >= 25) {
document.location.href = document.getElementsByClassName("m-select-sibling-episode").item(0)!.getElementsByTagName("a").item(1 || 0)!.href;
//Если дошли до конца то переходим по ссылке
if (countCurrentTime === countDurationTime && hrefChevronRight !== "/") {
//Проверить возможность сохранить в память состояние плеера, после перехода на следующую серию вернуть. fullscreen, play/pause
document.location.href = hrefChevronRight;
}
//Визуализируем счётчик
mainBlock.getElementsByTagName("a").item(0)!.style.setProperty("--width", (countTimerOut * 100) / 25 + "%");
mainBlock.getElementsByTagName("a").item(0)?.style.setProperty("--width", ((countDurationTime - countCurrentTime - 15) * 100 * -1) / 15 + "%");
}
} else {
//Если ещё не досмотрено 96% то ничего не делаем
countTimerOut = 0;
iFrame!.document.getElementById("answer_is_forty_two")?.remove();
iFrame?.document.getElementById("answer_is_forty_two")?.remove();
styleElem?.remove();
}
timerId = setTimeout(tick, 1000); // (*)
}, 1000);
timerId = setTimeout(tick, 500);
}, 500);

}
}
});

});

0 comments on commit c031973

Please sign in to comment.