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
+32 -19

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/*"]
} }
] ]
} }
+5 -5
View File
@@ -1,3 +1,4 @@
(function(securityToken, extensionId) {
function getButtons() { function getButtons() {
return document return document
.getElementById("menu-container") .getElementById("menu-container")
@@ -47,8 +48,8 @@ function setDislikes(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(),
@@ -59,8 +60,7 @@ function setState() {
// setLikes(response.likes); // setLikes(response.likes);
console.log(response); console.log(response);
setDislikes(formattedDislike); setDislikes(formattedDislike);
} else { } else {}
}
} }
); );
} }
@@ -119,4 +119,4 @@ function setEventListeners(evt) {
setEventListeners(); setEventListeners();
//window.addEventListener("hashchange", setEventListeners, false); })(document.currentScript.getAttribute("security-token"), document.currentScript.getAttribute("extension-id"));