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

Added support for ES modules #1

Open
wants to merge 9 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
123 changes: 20 additions & 103 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,23 @@
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 getSdarotCredentials = (key) => new Promise((res) => {
chrome.storage.sync.get([key], (result) => {
res(result[key]);
});
})
import core from './src/core.js'

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');
});
}
}
try {
console.log('background wrapper started')

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);
}
clearInterval(interval);
} else if (errElement?.length > 0 && errElement[0].textContent === SERVER_LOAD_ERROR_TEXT) {
console.log('Found loading error');
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();
clearInterval(interval);
}
}
}, 1000)
}

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url?.includes('episode')) {
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: true },
func: sdarotAutoPlay,
});
}
})
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log('tab updated, initializing autoplay');
core.initSdarotAutoplay(tabId, changeInfo)
})

chrome.webNavigation.onCommitted.addListener((details) => {
if (details.url?.includes("episode")) {
chrome.scripting.executeScript({
target: { tabId: details.tabId, allFrames: true },
func: sdarotAutoPlay,
});
}
}, {
url: [
{
hostContains: 'sdarot'
},
]
})
chrome.webNavigation.onCommitted.addListener((details) => {
console.log('url changed, initializing autoplay');
core.initSdarotAutoplay(details.tabId, details)
}, {
url: [
{
hostContains: 'sdarot'
},
]
})
} catch (e) {
console.error(e);
}
63 changes: 34 additions & 29 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
{
"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": "src/popup/popup.html"
},
"manifest_version": 3,
"version": "1.1.0",
"description": "Auto play sdarot episodes",
"permissions": [
"tabs",
"scripting",
"webNavigation",
"storage"
],
"minimum_chrome_version": "93",
AmitLacher marked this conversation as resolved.
Show resolved Hide resolved
"background": {
"service_worker": "background.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/",
"https://*.sdarot.tw/"
]
}
45 changes: 0 additions & 45 deletions popup.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SDAROT_USERNAME_KEY = 'sdarotUsername';
export const SDAROT_PASSWORD_KEY = 'sdarotPassword';
54 changes: 54 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as consts from "./consts.js";
import * as helpers from "./helpers.js";
import * as locators from "./locators.js";

const sdarotAutoPlay = () => {
const currentVersion = "1.1.0";
const probingTimeout = 1000;
let logging = false;

console.log(`Sdarot Auto Play version ${currentVersion} is Enabled`);

const interval = setInterval(async () => {
if (locators.IsNotLoggedErrorVisible()) {
console.log('Found user logging error, logging in...');
const [sdarotUsername, sdarotPassword] = await Promise.all(
helpers.getStoredCredentials(
[consts.SDAROT_USERNAME_KEY, consts.SDAROT_PASSWORD_KEY]
)
);

if (!logging && sdarotPassword && sdarotUsername) {
helpers.login(sdarotUsername, sdarotPassword);
logging = true;
}
clearInterval(interval);
} else if (locators.IsServerErrorVisible()) {
console.log('Found server loading error, reloading...');
window.location.reload();
} else {
if (locators.IsSpinnerVisible()) {
locators.GetEpisodeToPlay().click();
await helpers.sleep();

locators.GetPlayBtn().click();
await helpers.sleep();

locators.GetExpandViewBtn().click();

clearInterval(interval);
}
}
}, probingTimeout)
}

const initSdarotAutoplay = (tabId, info) => {
if (info.url?.includes('episode')) {
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: true },
func: sdarotAutoPlay,
});
}
}

export default initSdarotAutoplay;
47 changes: 47 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const getStoredCredentials = (keys) => {
console.log(`Getting values for keys: ${JSON.stringify(keys)}`);

return keys.map(key => new Promise((res) => {
chrome.storage.sync.get(key, (result) => {
res(result);
});
}));
}

export const sleep = (milliseconds = 1500) => {
return new Promise((res) => {
setTimeout(() => {
res();
}, milliseconds);
})
}

export const login = (username, password) => {
fetch("https://sdarot.buzz/login", {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to pass this function a baseURL of the current URL for this to work across multiple sites url.

"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');
});
}
Loading