Skip to content

New parser for edubar #21

@rrHakushi

Description

@rrHakushi

This one should work for teacher accounts.
Since JSON data sometimes in teacher accounts contained ');' which caused regex to stop early

  static parse(html) {
    let data = {
      _edubar: {},
    }

    // Use a more robust regex that anchors to the end of the line (multiline mode)
    // This prevents cutting off at ); inside JSON strings.
    const match = (html.match(/\.userhome\(([\s\S]+?)\);\s*$/m) || html.match(/\.userhome\(([\s\S]+?)\);/) || "")[1];
    if (!match) {
      throw new Error("Failed to parse Edupage data from html (userhome match not found)");
    }

    try {
      data = { ...JSON.parse(match) };
    } catch (e) {
      console.error("JSON parse error in userhome data:", e);
      throw new Error("Failed to parse JSON from Edupage html (.userhome)");
    }

    // Parse additional edubar data
    // Again, anchor to end of line to handle ); inside strings
    const match2 = (html.match(/edubar\(([\s\S]*?)\);\s*$/m) || html.match(/edubar\(([\s\S]*?)\);/) || "")[1];
    if (!match2) {
      console.warn("edubar data not found in html");
    } else {
      try {
        data._edubar = JSON.parse(match2) || {};
      } catch (e) {
        console.error("JSON parse error in edubar data:", e);
        // It's possible edubar is just too complex or broken, we can continue without it if userhome worked
      }
    }

    return data;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions