dteviot / WebToEpub

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

How to use EpubEditor to mass delete different types of <span></span> tags, but preserve the content within the tags #964

Closed norabelle101 closed 1 year ago

norabelle101 commented 1 year ago

Hello!

Apologies for another request (of sorts), but this can most definitely wait until you're free! :D This request relates a bit back to the latter half of issue #911. In that issue, you had kindly given me a script to use in EpubEditor to mass delete all tags, but to also preserve the text within those tags. I've tried a few things and that script seems to specifically work only with span class tags, but is it possible to somehow modify that script to also work with three different types of span tags?

Specifically:

<span style="">TEXT

<span id="">TEXT

<span xmlns="http://www.w3.org/1999/xhtml" class="koboSpan" id="kobo.29.4">TEXT

I would use Calibre to mass delete them, but they're not all the same, as the numbers within change per tag...

Here's three examples of the above three tags for a reference:

image

The numbers underlined in pink are the changing factor per tag (but this is just an example of having both class and id tags):

image

image

This is just because these tags are extremely cluttered in the EPUB and I can barely see the actual chapter text itself since there's thousands of them. But if this is too much trouble and time-consuming (or not possible to implement) then that's okay! :D Again, thank you so much for all your help thus far! :)

dteviot commented 1 year ago

@vanessabluellie This is the bit that selects <span> elements that have a class of tooltip-toggle

dom.querySelectorAll("span.tooltip-toggle")

This bit is key

"span.tooltip-toggle"

This is known as a CSS selector. By changing the selector, you can get different elements selected. More notes on CSS selectors.

norabelle101 commented 1 year ago

Ah I'm sorry I completely forgot to reply to this issue request. I did some testing for each span tag I wanted removed but didn't understand much of the CSS selectors so it didn't work ahah, but that's okay. I'll attempt to find a workaround regarding this, thank you anyway for all your help! :)

dteviot commented 1 year ago

@vanessabluellie Stupid question, are there any <span> tags that you don't want to have flattened? (i.e. remove the tag, but keep the content.) That is, if you just want all <span> tags flattened, then I can give you a script for that. e.g.

let toChange = [...dom.querySelectorAll("span")];
for(let s of toChange) {
    s.replaceWith(s.textContent);
}
return 0 < toChange.length;
norabelle101 commented 1 year ago

@dteviot

Ah not a stupid question at all, I should be apologising to you for making a irrelevant request. But still thank you for going out of your way to help me despite of everything! :D

Regarding the tags though, preferably I do it case by case so there are some tags that I don't want to have flattened at all. I was able to work it out a bit to just delete any type of tags that have the "class" attribute. But now I'm trying to figure out how to delete span tags with the "id" and "style" tag (which would require two different scripts since I delete them separately).

Or the much more difficult tag that is too difficult to get because of the "xmlns" tag...

But if this is a bit too taxing to personally create scripts for, then please do let me know and I will close my request immediately. Again, thank you so much for your continued help and effort! :D

dteviot commented 1 year ago

@vanessabluellie

But now I'm trying to figure out how to delete span tags with the "id" and "style" tag

Try this (note, I haven't tested it)

let toChange = [...dom.querySelectorAll("span")].filter(tag => tag.getAttribute("id") && tag.getAttribute("style"))
for(let s of toChange) {
    s.replaceWith(s.textContent);
}
return 0 < toChange.length;

Sorry for delay in responding,
I have an addiction problem with this game. https://www.satisfactorygame.com/

norabelle101 commented 1 year ago

No need to apologise! I'm very sorry for taking up much of your time! >_<

But regarding the EpubEditor script, I did try it on span "style" and "id" tags, but it didn't quite seem to work. But that's completely okay! I've bothered you with this irrelevant problem long enough ahah, thank you again for taking the time to help me! :)

dteviot commented 1 year ago

@vanessabluellie OK, I'll just close this then. Let me know when you want any more help.