[英]chrome extension not working on javascript contentScript
我一直在研究 chrome 擴展,早些時候我對 chrome 擴展做了一些更改,我在 contentScript 上添加了一些功能,從那時起,它一直沒有工作,我要么遇到意外的令牌錯誤,要么只是一般的語法問題,當涉及到 javascript / jquery 時,我不是最好的,我無法弄清楚問題出在哪里,我相信它在靠近底部的某個地方與 firebase 和腳本之間的代碼縮進有關,如果有人有的話想法我為什么會出錯,在哪里出錯,並且可以指出我正確的方向,我會非常感謝它,我在控制台中沒有收到任何錯誤只是不執行腳本並且不起作用
(function() {
'use strict';
let playerDataMap = new Map();
function loadPlayerDataFromCsv() {
console.log('**LOADING PLAYER DATA**');
$.get("https://raw.githubusercontent.com/TheWraith1912/Project1/main/iconsxb.csv", function(CSVdata) {
var lines = CSVdata.split("\n");
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
if (lines[i] != "") {
var currentline = lines[i].replaceAll('"', "").split(",");
var name = currentline[0].normalize("NFD").replace(/[\u0300-\u036f]/g, "").split(" ");
let key = name[name.length - 1] + currentline[1];
let value = currentline[2].trim();
playerDataMap.set(key, value);
}
}
});
console.log('**LOADED PLAYER DATA**');
}
function addStyleElement() {
if ($('style[title="highlightStyle"]').length > 0) {
return;
}
var t = document.createElement("style");
t.type = "text/css",
t.innerText = "\n .SearchResults.ui-layout-left .listFUTItem {\n height: 39px;\n }\n .SearchResults.ui-layout-left .listFUTItem .label {\n font-size: 10px;\n }\n .SearchResults.ui-layout-left .auction {\n margin-top: 0 !important;\n font-size: 12px;\n top: 4px;\n }\n",
document.head.appendChild(t);
}
function waitForSearchButton() {
console.log('**WAITING FOR TRANSFER SEARCH BUTTON**');
let searchButtonCandidate = $('.btn-standard.call-to-action');
if (searchButtonCandidate.length > 0 && searchButtonCandidate[0] && $(searchButtonCandidate[0]).text() === 'Search') {
console.log('**SEARCH BUTTON FOUND**');
$('.btn-standard.call-to-action').on("keyup", function(event) {
if (event.key == "b")
addStyleElement();
highlightValues();
});
console.log('**SEARCH BUTTON READY TO CLICK**');
} else {
setTimeout(function() {
waitForSearchButton();
}, 2000);
}
}
function highlightValues() {
setTimeout(function() {
console.log('Getting Everything Ready');
getPlayerDataFromSite();
//$('.pagination.prev').on('click', function(e){
$('.pagination.prev').keyup(function(e) {
if (e.keycode == 37)
e.preventDefault();
addStyleElement();
console.log('**PREVIOUS**');
setTimeout(function() {
getPlayerDataFromSite();
}, 500);
});
//$('.pagination.next').on('click', function(e){
$('.pagination.next').keyup(function(e) {
if (e.keycode == 39)
e.preventDefault();
addStyleElement();
console.log('**NEXT**');
setTimeout(function() {
getPlayerDataFromSite();
}, 500);
});
}, 1000);
}
function getPlayerDataFromSite() {
for (var i = 0; i <= 19; i++) {
var name = $('.name:eq(' + i + ')').text();
name = name.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
var rating = $('.rating:eq(' + i + ')').text();
var fullName = name.split(' ');
var sitePlayerPrice = '';
if ($('.auction:eq(' + i + ')').children('div').eq(2).text().split(":")[1] != undefined) {
sitePlayerPrice = $('.auction:eq(' + i + ')').children('div').eq(2).text().split(":")[1].replaceAll(",", "");
}
var excelDataSheetPrice = playerDataMap.get(fullName[fullName.length - 1] + "" + rating);
console.log('ExcelSheetPrice', fullName[fullName.length - 1] + "" + rating, "==>", excelDataSheetPrice);
console.log('SitePrice', name + "" + rating + "==>", sitePlayerPrice);
let sitePlayerPriceAsNumber = parseInt(sitePlayerPrice);
if (!excelDataSheetPrice || !sitePlayerPriceAsNumber) {
continue;
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 1.03)) {
console.log('**' + fullName + ' IS Orange NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'orange');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 1.01)) {
console.log('**' + fullName + ' IS GREY NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'grey');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice)) {
console.log('**' + fullName + ' IS GREEN NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'green');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 0.97)) {
console.log('**' + fullName + ' IS Magenta NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'magenta');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 0.80)) {
console.log('**' + fullName + ' IS RED NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'red');
}
}
}
loadPlayerDataFromCsv();
waitForSearchButton();
chrome.storage.local.get(["loggedIn"], function(result) {
if (result.loggedIn === true) {
func();
} else {
chrome.runtime.sendMessage({
cmd: "showNotification"
}, function(response) {});
}
})
}
)
您聲明了一個匿名函數(function(){...})
但就是這樣,您沒有運行它。 在末尾添加()
以執行您的函數: (function(){...})()
(function () {
'use strict';
let playerDataMap = new Map();
function loadPlayerDataFromCsv() {
console.log('**LOADING PLAYER DATA**');
$.get("https://raw.githubusercontent.com/TheWraith1912/Project1/main/iconsxb.csv", function (CSVdata) {
var lines = CSVdata.split("\n");
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
if (lines[i] != "") {
var currentline = lines[i].replaceAll('"', "").split(",");
var name = currentline[0].normalize("NFD").replace(/[\u0300-\u036f]/g, "").split(" ");
let key = name[name.length - 1] + currentline[1];
let value = currentline[2].trim();
playerDataMap.set(key, value);
}
}
});
console.log('**LOADED PLAYER DATA**');
}
function addStyleElement() {
if ($('style[title="highlightStyle"]').length > 0) {
return;
}
var t = document.createElement("style");
t.type = "text/css",
t.innerText = "\n .SearchResults.ui-layout-left .listFUTItem {\n height: 39px;\n }\n .SearchResults.ui-layout-left .listFUTItem .label {\n font-size: 10px;\n }\n .SearchResults.ui-layout-left .auction {\n margin-top: 0 !important;\n font-size: 12px;\n top: 4px;\n }\n",
document.head.appendChild(t);
}
function waitForSearchButton() {
console.log('**WAITING FOR TRANSFER SEARCH BUTTON**');
let searchButtonCandidate = $('.btn-standard.call-to-action');
if (searchButtonCandidate.length > 0 && searchButtonCandidate[0] && $(searchButtonCandidate[0]).text() === 'Search') {
console.log('**SEARCH BUTTON FOUND**');
$('.btn-standard.call-to-action').on("keyup", function (event) {
if (event.key == "b")
addStyleElement();
highlightValues();
});
console.log('**SEARCH BUTTON READY TO CLICK**');
} else {
setTimeout(function () {
waitForSearchButton();
}, 2000);
}
}
function highlightValues() {
setTimeout(function () {
console.log('Getting Everything Ready');
getPlayerDataFromSite();
//$('.pagination.prev').on('click', function(e){
$('.pagination.prev').keyup(function (e) {
if (e.keycode == 37)
e.preventDefault();
addStyleElement();
console.log('**PREVIOUS**');
setTimeout(function () {
getPlayerDataFromSite();
}, 500);
});
//$('.pagination.next').on('click', function(e){
$('.pagination.next').keyup(function (e) {
if (e.keycode == 39)
e.preventDefault();
addStyleElement();
console.log('**NEXT**');
setTimeout(function () {
getPlayerDataFromSite();
}, 500);
});
}, 1000);
}
function getPlayerDataFromSite() {
for (var i = 0; i <= 19; i++) {
var name = $('.name:eq(' + i + ')').text();
name = name.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
var rating = $('.rating:eq(' + i + ')').text();
var fullName = name.split(' ');
var sitePlayerPrice = '';
if ($('.auction:eq(' + i + ')').children('div').eq(2).text().split(":")[1] != undefined) {
sitePlayerPrice = $('.auction:eq(' + i + ')').children('div').eq(2).text().split(":")[1].replaceAll(",", "");
}
var excelDataSheetPrice = playerDataMap.get(fullName[fullName.length - 1] + "" + rating);
console.log('ExcelSheetPrice', fullName[fullName.length - 1] + "" + rating, "==>", excelDataSheetPrice);
console.log('SitePrice', name + "" + rating + "==>", sitePlayerPrice);
let sitePlayerPriceAsNumber = parseInt(sitePlayerPrice);
if (!excelDataSheetPrice || !sitePlayerPriceAsNumber) {
continue;
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 1.03)) {
console.log('**' + fullName + ' IS Orange NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'orange');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 1.01)) {
console.log('**' + fullName + ' IS GREY NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'grey');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice)) {
console.log('**' + fullName + ' IS GREEN NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'green');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 0.97)) {
console.log('**' + fullName + ' IS Magenta NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'magenta');
}
if (sitePlayerPriceAsNumber <= parseInt(excelDataSheetPrice * 0.80)) {
console.log('**' + fullName + ' IS RED NOW**')
$('.name:eq(' + i + ')').parent().css('background-color', 'red');
}
}
}
loadPlayerDataFromCsv();
waitForSearchButton();
chrome.storage.local.get(["loggedIn"], function (result) {
if (result.loggedIn === true) {
func();
} else {
chrome.runtime.sendMessage({
cmd: "showNotification"
}, function (response) { });
}
})
})() // <--- this
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.