dteviot / WebToEpub

A simple Chrome (and Firefox) Extension that converts Web Novels (and other web pages) into an EPUB.
Other
737 stars 139 forks source link

Royalroad 'Double chapter title' or removing title option #1528

Closed Leifman35 closed 1 month ago

Leifman35 commented 1 month ago

So i've seen this post: https://github.com/dteviot/WebToEpub/issues/1375 which was marked as completed a month ago.

Unfortunately, i don't see it as 'working' per se with royalroad and i can still see this happen for example with stories like https://www.royalroad.com/fiction/57271/tower-of-jack, for example: https://www.royalroad.com/fiction/57271/tower-of-jack/chapter/998941/chapter-48-the-killer-and-the-thief-part-1 or any other chapter from it would end with each chapter having double titles :(

I have no idea if the stuff previously discussed in the closed thread is still 'possible' but the idea of just having a 'tickable' option similar to others in the 'Advanced' options to basically 'remove chapter title' when someone tries to epub a story that has this double title happening to would be very useful i rekcon.

gamebeaker commented 1 month ago

@Leifman35 the problem is that the title in the chapter is not defined as a heading (https://www.w3schools.com/tags/tag_hn.asp). It is simple text in big letters.

gamebeaker commented 1 month ago

@Leifman35 the answer from @dteviot in #1375 still works. The problem is as far as i can tell that the Chapter you see doubled aren't really double. The script from @dteviot removes the double title in Chapter 48 but not in Chapter 53. Why? In Chapter 53 are the titles different. Chapter 53 - Flight of the Valkyries and Chapter 53 – Flight of the Valkyries - and are different.

Leifman35 commented 1 month ago

ah damn :( well as someone that is absolutely clueless technical wise (to the point i even opened the epub in calibre and tried to click 'Edit' and see how each html file code looked, and couldn't find any solution other than Manually edit/remove the lines for the title or the one below it, which to be honest is way too tedious for me..) it sucks :( I just wish there was an easy fix but i guess that's not the case.

Thank you still @gamebeaker for the explanation!

gamebeaker commented 1 month ago

@Leifman35 i found a solution. Download https://github.com/dteviot/EpubEditor Open main.html and upload the epub file Change the code to this:

function similarity(s1, s2) {
  var longer = s1;
  var shorter = s2;
  if (s1.length < s2.length) {
    longer = s2;
    shorter = s1;
  }
  var longerLength = longer.length;
  if (longerLength == 0) {
    return 1.0;
  }
  return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
}
function editDistance(s1, s2) {
  s1 = s1.toLowerCase();
  s2 = s2.toLowerCase();

  var costs = new Array();
  for (var i = 0; i <= s1.length; i++) {
    var lastValue = i;
    for (var j = 0; j <= s2.length; j++) {
      if (i == 0)
        costs[j] = j;
      else {
        if (j > 0) {
          var newValue = costs[j - 1];
          if (s1.charAt(i - 1) != s2.charAt(j - 1))
            newValue = Math.min(Math.min(newValue, lastValue),
              costs[j]) + 1;
          costs[j - 1] = lastValue;
          lastValue = newValue;
        }
      }
    }
    if (i > 0)
      costs[s2.length] = lastValue;
  }
  return costs[s2.length];
}
let title = dom.querySelector("h1");
let changed = (title != null);
if (changed) {
    for(let node of [...dom.querySelectorAll("p")]) {
        if (node.textContent == title.textContent || similarity(node.textContent, title.textContent) > 0.91) {
            console.log("remove:"+node.textContent)
            node.remove();
            continue;
        }
    }
}
return changed;

Click "run script above to modify Epub"

Leifman35 commented 1 month ago

@gamebeaker I could kiss you right now.

But in all seriousness, THANK YOU SO SO MUCH! Not only did it fix the issue but the step by step was so simple and easy that even i managed to do it issue free in a few seconds. i really appreciate the help! again, thank you. I really appreciate it.

dteviot commented 1 month ago

@Leifman35

Thanks to gamebeaker, EpubEditor is now available at https://dteviot.github.io/EpubEditor/, no need to download, unpack, etc.