From abd78ad0ff3d7caec68bc4c3f4deb4b295dd5197 Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 23:01:13 +0300 Subject: [PATCH 1/8] feat: added support for es modules Signed-off-by: Amit Lacher --- manifest.json | 62 +++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/manifest.json b/manifest.json index f1d09dc..381f20d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,30 +1,34 @@ { - "name": "Sdarot Auto Player", - "action": {"default_popup": "popup.html"}, - "manifest_version": 3, - "version": "1.0.0", - "description": "Auto play sdarot episodes", - "permissions": [ - "tabs", - "scripting", - "webNavigation", - "storage" - ], - "background": { - "service_worker": "background.js" - }, - "icons": { - "16": "assets/icons/icon.png", - "32": "assets/icons/icon-32.png", - "48": "assets/icons/icon-48.png", - "64": "assets/icons/icon-64.png", - "96": "assets/icons/icon-96.png", - "128": "assets/icons/icon-128.png", - "256": "assets/icons/icon-256.png" - }, - "host_permissions": [ - "https://*.sdarot.buzz/", - "https://*.sdarot.work/", - "https://*.sdarot.tv/" - ] - } \ No newline at end of file + "name": "Sdarot Auto Player", + "action": { + "default_popup": "popup.html" + }, + "manifest_version": 3, + "version": "1.1.0", + "description": "Auto play sdarot episodes", + "permissions": [ + "tabs", + "scripting", + "webNavigation", + "storage" + ], + "minimum_chrome_version": "93", + "background": { + "service_worker": "background-wrapper.js", + "type": "module" + }, + "icons": { + "16": "assets/icons/icon.png", + "32": "assets/icons/icon-32.png", + "48": "assets/icons/icon-48.png", + "64": "assets/icons/icon-64.png", + "96": "assets/icons/icon-96.png", + "128": "assets/icons/icon-128.png", + "256": "assets/icons/icon-256.png" + }, + "host_permissions": [ + "https://*.sdarot.buzz/", + "https://*.sdarot.work/", + "https://*.sdarot.tv/" + ] +} \ No newline at end of file From 9a4d459b8d489be0a7f0c1d39b3841a46451bfbc Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 22:58:46 +0300 Subject: [PATCH 2/8] feat: added consts file Signed-off-by: Amit Lacher --- consts.js | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 consts.js diff --git a/consts.js b/consts.js new file mode 100644 index 0000000..14e8fbc --- /dev/null +++ b/consts.js @@ -0,0 +1,4 @@ +const SDAROT_USERNAME_KEY = 'sdarotUsername'; +const SDAROT_PASSWORD_KEY = 'sdarotPassword'; + +export {SDAROT_USERNAME_KEY, SDAROT_PASSWORD_KEY}; \ No newline at end of file From cb53df7f68f489414f5ef7d777e2cfab676ead81 Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 22:59:05 +0300 Subject: [PATCH 3/8] feat: added locators file Signed-off-by: Amit Lacher --- locators.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 locators.js diff --git a/locators.js b/locators.js new file mode 100644 index 0000000..1482abf --- /dev/null +++ b/locators.js @@ -0,0 +1,38 @@ +const NOT_LOGGED_ERROR_TEXT = 'שגיאה 2!על מנת לצפות בפרק עליך להתחבר למערכת'; +const SERVER_LOAD_ERROR_TEXT= 'שגיאה 2!כל שרתי הצפייה שלנו עמוסים ולא יכולים לטפל בפניות נוספות, נא לנסות שנית מאוחר יותר.לצפייה בעומסי השרתים לחצו כאן.משתמשים שתרמו לאתר יכולים לצפות בפרקים גם בשעות העומס!'; + +function IsNotLoggedErrorVisible() { + const errElement = document.querySelectorAll(".err"); + return (errElement?.length > 0 && errElement[0].textContent === NOT_LOGGED_ERROR_TEXT) +} + +function IsServerErrorVisible() { + const errElement = document.querySelectorAll(".err"); + return (errElement?.length > 0 && errElement[0].textContent === SERVER_LOAD_ERROR_TEXT) +} + +function IsSpinnerVisible() { + const spinner = document.querySelector("#spinner"); + return !(spinner && window.getComputedStyle(spinner)?.display !== 'none'); +} + +function GetEpisodeToPlay() { + return document.querySelector("#proceed") +} + +function GetPlayBtn() { + return document.querySelector('.vjs-big-play-button > .vjs-icon-placeholder'); +} + +function GetExpandViewBtn() { + return document.querySelector('.vjs-fullscreen-control'); +} + +export { + IsNotLoggedErrorVisible, + IsServerErrorVisible, + IsSpinnerVisible, + GetEpisodeToPlay, + GetPlayBtn, + GetExpandViewBtn +}; \ No newline at end of file From 2fd6777c7f68d692262b984b6f0f0fede07a137d Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 22:59:31 +0300 Subject: [PATCH 4/8] feat: added helpers file Signed-off-by: Amit Lacher --- helpers.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 helpers.js diff --git a/helpers.js b/helpers.js new file mode 100644 index 0000000..7bf9c09 --- /dev/null +++ b/helpers.js @@ -0,0 +1,55 @@ +function getStoredCredentials(keys) { + console.log(`Getting values for keys: ${JSON.stringify(keys)}`); + + const values = []; + for (const key in keys){ + values.push(new Promise((res) => { + chrome.storage.sync.get(key, (result) => { + res(values.push(result)); + }); + })) + } + + + return values +} + +function sleep(milliseconds = 1500) { + return new Promise((res) => { + setTimeout(() => { + res(); + }, milliseconds); + }) +} + +function login(username, password) { + fetch("https://sdarot.buzz/login", { + "headers": { + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", + "accept-language": "he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7", + "cache-control": "no-cache", + "content-type": "application/x-www-form-urlencoded", + "pragma": "no-cache", + "sec-ch-ua": "\"Chromium\";v=\"106\", \"Google Chrome\";v=\"106\", \"Not;A=Brand\";v=\"99\"", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "\"Windows\"", + "sec-fetch-dest": "document", + "sec-fetch-mode": "navigate", + "sec-fetch-site": "same-origin", + "sec-fetch-user": "?1", + "upgrade-insecure-requests": "1" + }, + "referrer": "https://sdarot.buzz/", + "referrerPolicy": "strict-origin", + "body": `location=index&username=${username}&password=${password}&submit_login=`, + "method": "POST", + "mode": "cors", + "credentials": "include" + }).then(() => { + window.location.reload(); + }).catch((error) => { + console.error('Error while trying to log in'); + }); +} + +export {getStoredCredentials, sleep, login}; \ No newline at end of file From 647cbd2c7eca1e57be19cd791a5fa3dbb81383e9 Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 23:00:16 +0300 Subject: [PATCH 5/8] feat: updated background Signed-off-by: Amit Lacher --- background.js | 122 ++++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 88 deletions(-) diff --git a/background.js b/background.js index 25971e3..7eeca59 100644 --- a/background.js +++ b/background.js @@ -1,106 +1,52 @@ +import * as consts from "/consts.js"; +import * as helpers from "/helpers.js"; +import * as locators from "/locators.js"; + function sdarotAutoPlay() { - console.log('Sdarot Auto Play is Enabled'); - let isSpinnerHiddenChange = false; - let isTryingToLog = false; - const SDAROT_USERNAME_KEY = 'sdarotUsername'; - const SDAROT_PASSWORD_KEY = 'sdarotPassword'; - const NOT_LOGGED_ERROR_TEXT = 'שגיאה 2!על מנת לצפות בפרק עליך להתחבר למערכת'; - const SERVER_LOAD_ERROR_TEXT = 'שגיאה 2!כל שרתי הצפייה שלנו עמוסים ולא יכולים לטפל בפניות נוספות, נא לנסות שנית מאוחר יותר.לצפייה בעומסי השרתים לחצו כאן.משתמשים שתרמו לאתר יכולים לצפות בפרקים גם בשעות העומס!'; - const sleep = (miliseconds) => new Promise((res) => { - setTimeout(() => { - res(); - }, miliseconds); - }) + const currentVersion = "1.1.0"; + const probingTimeout = 1000; + let probCount = 0; + let logging = false; - const getSdarotCredentials = (key) => new Promise((res) => { - chrome.storage.sync.get([key], (result) => { - res(result[key]); - }); - }) - - const login = (username, password) => { - if (!isTryingToLog) { - isTryingToLog = true; - fetch("https://sdarot.buzz/login", { - "headers": { - "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", - "accept-language": "he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7", - "cache-control": "no-cache", - "content-type": "application/x-www-form-urlencoded", - "pragma": "no-cache", - "sec-ch-ua": "\"Chromium\";v=\"106\", \"Google Chrome\";v=\"106\", \"Not;A=Brand\";v=\"99\"", - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-platform": "\"Windows\"", - "sec-fetch-dest": "document", - "sec-fetch-mode": "navigate", - "sec-fetch-site": "same-origin", - "sec-fetch-user": "?1", - "upgrade-insecure-requests": "1" - }, - "referrer": "https://sdarot.buzz/", - "referrerPolicy": "strict-origin", - "body": `location=%2Fwatch%2F8327-%25D7%2591%25D7%2599%25D7%25AA-%25D7%2594%25D7%2593%25D7%25A8%25D7%25A7%25D7%2595%25D7%259F-house-of-the-dragon%2Fseason%2F1%2Fepisode%2F9&username=${username}&password=${password}&submit_login=`, - "method": "POST", - "mode": "cors", - "credentials": "include" - }).then(() => { - window.location.reload(); - }).catch((error) => { - console.log('Error while trying to log in'); - }); - } - } + console.log(`Sdarot Auto Play version ${currentVersion} is Enabled`); const interval = setInterval(async () => { - const errElement = document.querySelectorAll(".err"); - if (errElement?.length > 0 && errElement[0].textContent === NOT_LOGGED_ERROR_TEXT) { - console.log('You need to log in first'); - const sdarotUsername = await getSdarotCredentials(SDAROT_USERNAME_KEY); - const sdarotPassword = await getSdarotCredentials(SDAROT_PASSWORD_KEY); - if (sdarotPassword && sdarotUsername) { - login(sdarotUsername, sdarotPassword); + console.log(`Searching for errors. Probing count is ${probCount}`) + if (locators.IsNotLoggedErrorVisible()) { + console.log('Found user logging error, logging in...'); + const [sdarotUsername, sdarotPassword] = helpers.getStoredCredentials([consts.SDAROT_USERNAME_KEY, consts.SDAROT_PASSWORD_KEY]); + + if (!logging && sdarotPassword && sdarotUsername) { + helpers.login(sdarotUsername, sdarotPassword); + logging = true; } clearInterval(interval); - } else if (errElement?.length > 0 && errElement[0].textContent === SERVER_LOAD_ERROR_TEXT) { - console.log('Found loading error'); + } else if (locators.IsServerErrorVisible()) { + console.log('Found server loading error, reloading...'); window.location.reload(); } else { - const spinner = document.querySelector("#spinner"); - if (spinner && window.getComputedStyle(spinner)?.display !== 'none') { - isSpinnerHiddenChange = true; - } else if (isSpinnerHiddenChange) { - const playEpisode = document.querySelector("#proceed"); - playEpisode.click(); - await sleep(1500); - document.querySelector('.vjs-big-play-button > .vjs-icon-placeholder').click(); - await sleep(1500); - document.querySelector('.vjs-fullscreen-control').click(); + if (locators.IsSpinnerVisible()) { + locators.GetEpisodeToPlay().click(); + await helpers.sleep(); + + locators.GetPlayBtn().click(); + await helpers.sleep(); + + locators.GetExpandViewBtn().click(); + clearInterval(interval); } } - }, 1000) + }, probingTimeout) } -chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { - if (changeInfo.url?.includes('episode')) { +function initSdarotAutoplay(tabId, info) { + if (info.url?.includes('episode')) { chrome.scripting.executeScript({ - target: { tabId: tabId, allFrames: true }, + target: {tabId: tabId, allFrames: true}, func: sdarotAutoPlay, }); } -}) +} -chrome.webNavigation.onCommitted.addListener((details) => { - if (details.url?.includes("episode")) { - chrome.scripting.executeScript({ - target: { tabId: details.tabId, allFrames: true }, - func: sdarotAutoPlay, - }); - } -}, { - url: [ - { - hostContains: 'sdarot' - }, - ] -}) \ No newline at end of file +export {initSdarotAutoplay} \ No newline at end of file From b095f6eef352224047d2518f9e06571ec5ecce92 Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 23:02:10 +0300 Subject: [PATCH 6/8] feat: added wrapper Signed-off-by: Amit Lacher --- background-wrapper.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 background-wrapper.js diff --git a/background-wrapper.js b/background-wrapper.js new file mode 100644 index 0000000..24564c5 --- /dev/null +++ b/background-wrapper.js @@ -0,0 +1,23 @@ +import * as background from '/background.js' + +try { + console.log('background wrapper started') + + chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { + console.log('tab updated, initializing autoplay'); + background.initSdarotAutoplay(tabId, changeInfo) + }) + + chrome.webNavigation.onCommitted.addListener((details) => { + console.log('url changed, initializing autoplay'); + background.initSdarotAutoplay(details.tabId, details) + }, { + url: [ + { + hostContains: 'sdarot' + }, + ] + }) +} catch (e) { + console.error(e); +} \ No newline at end of file From c9f20d2f219bc9bb4dab8c9d6370d9fa5c7d9e36 Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 23:02:27 +0300 Subject: [PATCH 7/8] feat: added support for modules Signed-off-by: Amit Lacher --- popup.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/popup.html b/popup.html index e1a0459..d87619f 100644 --- a/popup.html +++ b/popup.html @@ -1,7 +1,7 @@ - + From 6a96355af7485865059609437e30736e8ef24c90 Mon Sep 17 00:00:00 2001 From: Amit Lacher Date: Sat, 6 May 2023 23:03:07 +0300 Subject: [PATCH 8/8] feat: updated popup functions Signed-off-by: Amit Lacher --- popup.js | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/popup.js b/popup.js index 8a107b8..518fa6b 100644 --- a/popup.js +++ b/popup.js @@ -1,35 +1,13 @@ -const SDAROT_USERNAME_KEY = 'sdarotUsername'; -const SDAROT_PASSWORD_KEY = 'sdarotPassword'; +import * as consts from "/consts.js"; +import * as helpers from "/helpers.js"; -const getSdarotCredentials = (key) => new Promise((res) => { - chrome.storage.sync.get([key], (result) => { - res(result[key]); - }); -}) +function loadCredentials() { + const [sdarotUsername, sdarotPassword] = helpers.getStoredCredentials([consts.SDAROT_USERNAME_KEY, consts.SDAROT_PASSWORD_KEY]); -const onSave = () => { - const username = document.getElementById("sdarot-auto-username").value; - if (username) { - chrome.storage.sync.set({ sdarotUsername: username }); - } - - const password = document.getElementById("sdarot-auto-password").value; - if (password) { - chrome.storage.sync.set({ sdarotPassword: password }); - } - - const popup = document.getElementById('save-popup'); - popup.classList.remove('hidden'); - popup.classList.add('popup-fade'); -} - -document.addEventListener('DOMContentLoaded', async () => { - const sdarotUsername = await getSdarotCredentials(SDAROT_USERNAME_KEY); if (sdarotUsername) { document.getElementById("sdarot-auto-username").value = sdarotUsername; } - const sdarotPassword = await getSdarotCredentials(SDAROT_PASSWORD_KEY); if (sdarotPassword) { document.getElementById("sdarot-auto-password").value = sdarotPassword; } @@ -42,4 +20,23 @@ document.addEventListener('DOMContentLoaded', async () => { popup.classList.add('hidden'); popup.classList.remove('popup-fade'); }) -}) \ No newline at end of file +} + + +const onSave = () => { + const username = document.getElementById("sdarot-auto-username").value; + if (username) { + chrome.storage.sync.set({sdarotUsername: username}); + } + + const password = document.getElementById("sdarot-auto-password").value; + if (password) { + chrome.storage.sync.set({sdarotPassword: password}); + } + + const popup = document.getElementById('save-popup'); + popup.classList.remove('hidden'); + popup.classList.add('popup-fade'); +} + +document.addEventListener('DOMContentLoaded', loadCredentials) \ No newline at end of file