2

(I'm more of a python developer, so forgive me) I was attempting to implement a Repositories code which would be added to chrome and act as a downloading service, and when attempting to utilize it chrome spit back an error Uncaught SyntaxError: Unexpected token < in JSON at position 0 with a stack trace of _generated_background_page.html:1 (anonymous function).

I couldn't find any pull requests which solved this, nor could I find any less than symbol on line 0. I did change the original code, however this code did not work in the first place, and I only beautified it as it was condensed to a single line. For reference, this is what my code looks like:

function init() {
    chrome.tabs.executeScript({
        allFrames: !0,
        code: "document.documentElement.outerHTML"
    }, function(a) {
        parseHTML(a[0])
    })
}

function parseHTML(a) {
    var b = "document/",
        c = "ratings/",
        d = a.indexOf(b),
        e = a.indexOf(c),
        f = a.substring(d + b.length, e - 1);
    req(uUrl), req(fUrl + f), fLink = bUrl + fUrl + dUrl + f
}

function req(a) {
    var b = new XMLHttpRequest;
    b.open("GET", bUrl + a, !0), b.onload = function() {
        if (b.status >= 200 || b.status <= 400) {
            var a = JSON.parse(b.responseText);
            console.log(a), manageData(a)
        } else console.log("Bad response from server.")
    }, b.send()
}

function manageData(a) {
    a.user_id ? uInfo = a : a.db_filename ? (fInfo = a, cID = fInfo.course_id, getAll ? req(cUrl + cID + limit) : download()) : getAll && (cInfo = a, download())
}

function download() {
    if (getAll) {
        var a = bUrl + fUrl + dUrl;
        console.log("Trying cID" + cID);
        for (file in cInfo) console.log(file, a + cInfo[file].db_filename, cInfo[file].title), chrome.downloads.download({
            url: a + cInfo[file].db_filename
        })
    } else console.log(fLink, fInfo.title, fInfo.course.dept_acro, fInfo.course.course_num), chrome.downloads.download({
        url: fLink
    })
}
var bUrl = "https://www.coursehero.com/api/v1/",
    uUrl = "users/",
    fUrl = "documents/",
    dUrl = "download/",
    cUrl = "documents/course/",
    fLink, uInfo, fInfo, cInfo, limit = "/?limit=1000",
    getAll = !1;
chrome.runtime.onInstalled.addListener(function() {
    chrome.contextMenus.create({
        title: "Download this file.",
        id: "getOne",
        contexts: ["all"]
    }), chrome.contextMenus.create({
        title: "Download all files from this course.",
        id: "getAll",
        contexts: ["all"]
    })
}), chrome.contextMenus.onClicked.addListener(function(a, b) {
    "getOne" === a.menuItemId ? getAll = !1 : "getAll" === a.menuItemId && (getAll = !0), init()
});

I'm assuming since there's no physical symbol at position 0, this is a backend problem which I am unequipped to deal with, as I've never written in javascript before.

  • Presumably the problem is with `var a = JSON.parse(b.responseText);` Your script is expecting JSON, but the server is returning HTML and your script does not check contents before attempting to parse. Use your browser inspector to see the request and response happening. Ensure your server is properly configured to return JSON. – miken32 Dec 07 '20 at 22:38

0 Answers0