Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CoroCoro connector #7472

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add CoroCoro connector #7472

wants to merge 4 commits into from

Conversation

viliml
Copy link
Contributor

@viliml viliml commented Oct 10, 2024

No description provided.

Copy link
Contributor

@MikeZeDev MikeZeDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work but

  • Dont assume the script you want is the nth script in all the page scripts. Perform checks on the script content instead.

  • I dont like those eval at all. If you want to use eval perhaps you should use them in browser context i.e injecting a script using Engine.Request.fetchUI. Perhaps is event easier this way, if you are able to parse next_f array idk.

src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
src/web/mjs/connectors/CoroCoro.mjs Outdated Show resolved Hide resolved
@viliml viliml requested a review from MikeZeDev October 10, 2024 20:09
async _getMangas() {
/** @type {HTMLScriptElement[]} */
const scripts = await this.fetchDOM(this.url + '/rensai', 'script');
for (const scriptElem of scripts.reverse()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to loop by yourself if you can identify the script by content

const scripts = [...document.querySelectorAll('script')];
const script = scripts.find(script=> script.innerText.indexOf('totalChapterLikes') > 0)?.innerText;
const json = JSON.parse(script.substring(22, script.length - 2));
const data = JSON.parse(json.substring(json.indexOf(':') + 1))[3]['children'][3]['weekdays'];

/** @type {string} */
const json = JSON.parse(script.substring(22, script.length - 2));
/** @type {Object<string, {id: number, title: string}[]>} */
const data = JSON.parse(json.substring(json.indexOf(':') + 1))[3]['children'][0][3]['children'][1][3]['section']['chapters'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum... Thats a lot of fixed indexes :/
Since the position of "chapters" in the json object may vary, why not trying to locate it dynamically?

function locate(obj, keyname)
  {
    if (!obj) return null;
    if (obj[keyname]) {
      return (obj[keyname]);
    }
    
   let result = null;
 
    for (i in obj) {
      if (typeof obj[i] === 'object')
       result = result ?? locate(obj[i], keyname);
    }
    return result;

  }

const scripts = [...document.querySelectorAll('script')];
const script = scripts.find(script=> script.innerText.indexOf('omittedMiddleChapters') > 0)?.innerText;
const json = JSON.parse(script.substring(22, script.length - 2));
const data = JSON.parse(json.substring(json.indexOf(':') + 1));
 const chapters = locate(data, 'chapters');

Or if you fear that "chapters" wont be unique you may want to "locate" 'earlyChapters', 'omittedMiddleChapters' and 'latestChapters'. I doubt there will be duplicates.

And maybe, do that for the other JSON parsing, idk?

const script = scriptElem.innerText;
try {
/** @type {string} */
const json = JSON.parse(script.substring(22, script.length - 2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposition :

function locate(obj, keyname)
  {
    if (!obj) return null;
    if (obj[keyname]) {
      return (obj[keyname]);
    }
    
   let result = null;
 
    for (i in obj) {
      if (typeof obj[i] === 'object')
       result = result ?? locate(obj[i], keyname);
    }
    return result;

  }

const scripts = [...document.querySelectorAll('script')];
const script = scripts.find(script=> script.innerText.indexOf('viewerSection') > 0)?.innerText;
const json = JSON.parse(script.substring(22, script.length - 2));
const data = JSON.parse(json.substring(json.indexOf(':') + 1));
const pages = locate(data, 'viewerSection')?.pages;

const script = scriptElem.innerText;
try {
/** @type {string} */
const json = JSON.parse(script.substring(22, script.length - 2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, since JSON parsing first steps are the same perhaps we should make a function?

function findAndParseJSON(scripts, textToFind) {
  const script = scripts.find(script=> script.innerText.indexOf(textToFind) > 0)?.innerText;
  const json = JSON.parse(script.substring(22, script.length - 2));
  return JSON.parse(json.substring(json.indexOf(':') + 1))
}

const scripts = [...document.querySelectorAll('script')];
const data = findAndParseJSON( scripts, 'weekdays');

@viliml
Copy link
Contributor Author

viliml commented Oct 12, 2024

The "allow edits by maintainers" checkbox is checked, since you're already writing full snippets of fixed code in your comments you might as well commit them.

@MikeZeDev
Copy link
Contributor

I have no time to spare and no intention to add such complex website to Hakuneko anymore. Its better for the team and for everyone to spend time on Haruneko where this website was added and working until its last update.

@ronny1982
Copy link
Contributor

Suggestion to close PR after Thursday, 2 January 2025 (12 weeks) if not approved by a reviewer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants