简体   繁体   中英

chrome extension not working on javascript contentScript

i have been working on a chrome extension and earlier i made some changes to the chrome extension , i added in a couple feautres on the contentScript and ever since then , it has hasnt been working ive been either getting unexpected token errors or just general syntax issues , i am not the best when it comes to javascript / jquery and i cant figure out where the issue lies , i beleive it is somewhere near the bottom to do with the indentation of the code between the firebase and the script , if anyone has any ideas why and where i am going wrong and can point me in the right direction i would greatly appricitate it , i do not get any errors in the console just doesnt execute the script and doesnt work

(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) {});

        }
    })
}
)

 

You declare an anonymous function (function(){...}) but that's it, you don't run it. Add () at the end to execute your 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM