Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

original version 1.1.0 (close PR when done) #3

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions background-wrapper.js
Original file line number Diff line number Diff line change
@@ -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);
}
122 changes: 34 additions & 88 deletions background.js
Original file line number Diff line number Diff line change
@@ -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'
},
]
})
export {initSdarotAutoplay}
4 changes: 4 additions & 0 deletions consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const SDAROT_USERNAME_KEY = 'sdarotUsername';
const SDAROT_PASSWORD_KEY = 'sdarotPassword';

export {SDAROT_USERNAME_KEY, SDAROT_PASSWORD_KEY};
55 changes: 55 additions & 0 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -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};
38 changes: 38 additions & 0 deletions locators.js
Original file line number Diff line number Diff line change
@@ -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
};
62 changes: 33 additions & 29 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -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/"
]
}
"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/"
]
}
2 changes: 1 addition & 1 deletion popup.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="popup.js"></script>
<script src="popup.js" type="module"></script>
<link rel="stylesheet" href="popup.css"/>
</head>
<body>
Expand Down
Loading