Change chrome js injection method

This commit is contained in:
mynovelhost committed 2021-11-18 18:22:53 +01:00
1 parent eb1756cce9
commit 50028762fb
4 files changed
+70 -57

No files matched your search

+7 -12
View File
@@ -1,6 +1,12 @@
const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net"; const apiUrl = "https://return-youtube-dislike-api.azurewebsites.net";
const securityToken = Math.random().toString(36).substring(2, 15);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse(securityToken);
});
chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
if (request.securityToken === securityToken) {
if (request.message === "get_auth_token") { if (request.message === "get_auth_token") {
// chrome.identity.getAuthToken({ interactive: true }, function (token) { // chrome.identity.getAuthToken({ interactive: true }, function (token) {
// console.log(token); // console.log(token);
@@ -12,7 +18,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// console.log("logging off"); // console.log("logging off");
// chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off")); // chrome.identity.clearAllCachedAuthTokens(() => console.log("logged off"));
} else if (request.message == "set_state") { } else if (request.message == "set_state") {
console.log(request); // console.log(request);
// chrome.identity.getAuthToken({ interactive: true }, function (token) { // chrome.identity.getAuthToken({ interactive: true }, function (token) {
let token = ""; let token = "";
fetch(`${apiUrl}/votes?videoId=${request.videoId}`, { fetch(`${apiUrl}/votes?videoId=${request.videoId}`, {
@@ -31,16 +37,5 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
//}); //});
return true; return true;
} }
});
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status == "complete") {
if (tab.url && tab.url.indexOf("youtube.") < 0) return;
chrome.tabs.get(tabId, (tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["script.js"],
});
});
} }
}); });
+10
View File
@@ -0,0 +1,10 @@
chrome.runtime.sendMessage({}, (securityToken) => {
var script = document.createElement('script');
script.setAttribute("security-token", securityToken);
script.setAttribute("extension-id", chrome.runtime.id);
script.src = chrome.runtime.getURL('script.js');
script.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
});
+10 -2
View File
@@ -6,7 +6,6 @@
"background": { "background": {
"service_worker": "background.js" "service_worker": "background.js"
}, },
"permissions": ["activeTab", "scripting"],
"host_permissions": ["*://*.youtube.com/*"], "host_permissions": ["*://*.youtube.com/*"],
"action": { "action": {
"default_popup": "popup.html" "default_popup": "popup.html"
@@ -19,7 +18,16 @@
{ {
"matches": ["*://*.youtube.com/*"], "matches": ["*://*.youtube.com/*"],
"js": ["content-script.js"], "js": ["content-script.js"],
"run_at": "document_idle" "run_at": "document_start"
}
],
"externally_connectable": {
"matches": ["*://*.youtube.com/*"]
},
"web_accessible_resources": [
{
"resources": ["script.js"],
"matches": ["*://*.youtube.com/*"]
} }
] ]
} }
+43 -43
View File
@@ -1,34 +1,35 @@
function getButtons() { (function(securityToken, extensionId) {
function getButtons() {
return document return document
.getElementById("menu-container") .getElementById("menu-container")
?.querySelector("#top-level-buttons-computed"); ?.querySelector("#top-level-buttons-computed");
} }
function getLikeButton() { function getLikeButton() {
return getButtons().children[0]; return getButtons().children[0];
} }
function getDislikeButton() { function getDislikeButton() {
return getButtons().children[1]; return getButtons().children[1];
} }
function isVideoLiked() { function isVideoLiked() {
return getLikeButton().classList.contains("style-default-active"); return getLikeButton().classList.contains("style-default-active");
} }
function isVideoDisliked() { function isVideoDisliked() {
return getDislikeButton().classList.contains("style-default-active"); return getDislikeButton().classList.contains("style-default-active");
} }
function isVideoNotLiked() { function isVideoNotLiked() {
return getLikeButton().classList.contains("style-text"); return getLikeButton().classList.contains("style-text");
} }
function isVideoNotDisliked() { function isVideoNotDisliked() {
return getDislikeButton().classList.contains("style-text"); return getDislikeButton().classList.contains("style-text");
} }
function getState() { function getState() {
if (isVideoLiked()) { if (isVideoLiked()) {
return "liked"; return "liked";
} }
@@ -36,69 +37,68 @@ function getState() {
return "disliked"; return "disliked";
} }
return "neutral"; return "neutral";
} }
function setLikes(likesCount) { function setLikes(likesCount) {
getButtons().children[0].querySelector("#text").innerText = likesCount; getButtons().children[0].querySelector("#text").innerText = likesCount;
} }
function setDislikes(dislikesCount) { function setDislikes(dislikesCount) {
getButtons().children[1].querySelector("#text").innerText = dislikesCount; getButtons().children[1].querySelector("#text").innerText = dislikesCount;
} }
function setState() { function setState() {
chrome.runtime.sendMessage( chrome.runtime.sendMessage(extensionId, {
{ securityToken: securityToken,
message: "set_state", message: "set_state",
videoId: getVideoId(), videoId: getVideoId(),
state: getState(), state: getState(),
}, },
function (response) { function(response) {
if (response != undefined) { if (response != undefined) {
const formattedDislike = numberFormat(response.dislikes); const formattedDislike = numberFormat(response.dislikes);
// setLikes(response.likes); // setLikes(response.likes);
console.log(response); console.log(response);
setDislikes(formattedDislike); setDislikes(formattedDislike);
} else { } else {}
}
} }
); );
} }
function likeClicked() { function likeClicked() {
console.log("like" + getState()); console.log("like" + getState());
setState(); setState();
} }
function dislikeClicked() { function dislikeClicked() {
console.log("dislike" + getState()); console.log("dislike" + getState());
setState(); setState();
} }
function setInitalState() { function setInitalState() {
setState(); setState();
} }
function getVideoId() { function getVideoId() {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const videoId = urlParams.get("v"); const videoId = urlParams.get("v");
return videoId; return videoId;
} }
function isVideoLoaded() { function isVideoLoaded() {
const videoId = getVideoId(); const videoId = getVideoId();
return ( return (
document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null document.querySelector(`ytd-watch-flexy[video-id='${videoId}']`) !== null
); );
} }
function numberFormat(numberState) { function numberFormat(numberState) {
const userLocales = navigator.language; const userLocales = navigator.language;
const formatter = Intl.NumberFormat(userLocales, { notation: "compact" }); const formatter = Intl.NumberFormat(userLocales, { notation: "compact" });
return formatter.format(numberState); return formatter.format(numberState);
} }
function setEventListeners(evt) { function setEventListeners(evt) {
function checkForJS_Finish() { function checkForJS_Finish() {
if (getButtons()?.offsetParent && isVideoLoaded()) { if (getButtons()?.offsetParent && isVideoLoaded()) {
clearInterval(jsInitChecktimer); clearInterval(jsInitChecktimer);
@@ -115,8 +115,8 @@ function setEventListeners(evt) {
if (window.location.href.indexOf("watch?") >= 0) { if (window.location.href.indexOf("watch?") >= 0) {
var jsInitChecktimer = setInterval(checkForJS_Finish, 111); var jsInitChecktimer = setInterval(checkForJS_Finish, 111);
} }
} }
setEventListeners(); setEventListeners();
//window.addEventListener("hashchange", setEventListeners, false); })(document.currentScript.getAttribute("security-token"), document.currentScript.getAttribute("extension-id"));