Tutorials & Resources God damn google translate text blobs

Discussion in 'Novel General' started by Fordask, Jan 21, 2016.

Thread Status:
Not open for further replies.
  1. Fordask

    Fordask I feel like having a Bacon Pizza

    Joined:
    Nov 12, 2015
    Messages:
    159
    Likes Received:
    201
    Reading List:
    Link
    So I started reading MTLs and the fact the google translate screws up the formatting and gives you a huge blob of text bugged the hell out of me.

    So I wrote the following tampermonkey script (http://tampermonkey.net/) for 17k.com(Only works on 17k.com):
    Code:
    // ==UserScript==
    // @name         17k-google translate fix
    // @version      0.1
    // @description  Wrap text bocks on 17k in p tags so google translate wont skrew up the format
    // @author       Me
    // @match        http://*.17k.com/chapter/*
    // @grant        none
    // ==/UserScript==
    /* jshint -W097 */
    'use strict';
    
    // Your code here...
    var orignalstring = document.getElementById("chapterContentWapper").innerHTML;
    var newstring = "<p>" + orignalstring.replace(new RegExp("<br><br>", "g"), "</p><br><p>") + "</p>";
    document.getElementById("chapterContentWapper").innerHTML = newstring;
    
    To turn this:
    [​IMG]

    Into this:
    [​IMG]

    Edit (This version is a bit more flexible. It should work on most sites if you change the @match):
    Code:
    // ==UserScript==
    // @name         sitename-google translate fix
    // @version      0.1
    // @description  Replace line breaks with divs so google translate wont screw up the format
    // @author       Me
    // @match        http://*.sitename.com/*
    // @grant        none
    // ==/UserScript==
    /* jshint -W097 */
    'use strict';
    
    // Your code here...
    var orignalstring = document.getElementsByTagName('body')[0].innerHTML;
    var newstring = orignalstring.replace(new RegExp("<br>", "g"), '<div style="margin-bottom:1rem;"></div>');
    document.getElementsByTagName("body")[0].innerHTML = newstring;
    
    edit2: This version ends up breaking less sites when you match all and accidentally leave it on:
    Code:
    // ==UserScript==
    // @name         GT-fix
    // @version      0.2
    // @description  Replace line breaks with divs so google translate wont screw up the format
    // @author       You
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    /* jshint -W097 */
    'use strict';
    
    var elementList = document.querySelectorAll('br');
    for (var i = 0; i < elementList.length; i++) {
        if (elementList[i]) {
            elementList[i].outerHTML = '<div style="height:1rem;padding:0;margin:0;"></div>';
        }
    }
    
     
    Last edited: Jan 25, 2016
    Astaroth, blee8, kainee and 10 others like this.
  2. achacko

    achacko Well-Known Member

    Joined:
    Oct 25, 2015
    Messages:
    57
    Likes Received:
    7
    Reading List:
    Link
    how would i use this, for those of us not big on tech? iv started reading some MTLs cause the translation aret slow
     
  3. kryie

    kryie Well-Known Member

    Joined:
    Nov 19, 2015
    Messages:
    48
    Likes Received:
    20
    Reading List:
    Link
    a genius ah,

    I always use bing because of this proplem

    now can you please explain what to do to make it like that?
     
    Fordask likes this.
  4. Fordask

    Fordask I feel like having a Bacon Pizza

    Joined:
    Nov 12, 2015
    Messages:
    159
    Likes Received:
    201
    Reading List:
    Link
    Step 1) Install tampermonkey (https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en)
    Step 2) Click on the tampermonkey button that was added to the right of the omnibar
    Step 3) Click on "Add new script..."
    Step 4) Copy and paste the code in the OP
    Step 5) If using the second script change sitename for @match and @name to whatever site you want
    Step 6) Save
    Step 7) See if it works. It might it might not. If not you are stuck using bing.
     
    Last edited: Jan 22, 2016
  5. kryie

    kryie Well-Known Member

    Joined:
    Nov 19, 2015
    Messages:
    48
    Likes Received:
    20
    Reading List:
    Link
    by the way, what is the name of that novel?
     
  6. Fordask

    Fordask I feel like having a Bacon Pizza

    Joined:
    Nov 12, 2015
    Messages:
    159
    Likes Received:
    201
    Reading List:
    Link
    Wicked Solider King
     
    Raksha and blee8 like this.
  7. LordCorwin

    LordCorwin Supreme Book Lord; Leader of the Fiction Faction

    Joined:
    Nov 11, 2015
    Messages:
    1,090
    Likes Received:
    1,089
    Reading List:
    Link
    I don't know if you're the first to do something like this or not, but seeing as you're the first that I've seen to do it so easily and ably, I must offer my appreciation.

    THANK YOU MASTER! YOUR DISCIPLE AWAITS FURTHER REVELATIONS!:D
     
  8. Dr.Maniac

    Dr.Maniac I am Death.

    Joined:
    Oct 20, 2015
    Messages:
    198
    Likes Received:
    250
    Reading List:
    Link
    What software do you use for MTL? is it the built in translator for chrome?
     
  9. Fordask

    Fordask I feel like having a Bacon Pizza

    Joined:
    Nov 12, 2015
    Messages:
    159
    Likes Received:
    201
    Reading List:
    Link
    Yeah built in chrome translater.
    I didnt try bing yet...... It works much better. No need for crappy quickfixes. Screw google translate. Screw tampermonkey scripts. Im using bing translate from now on.
     
    blee8 and Random like this.
  10. mllhild

    mllhild Active Member

    Joined:
    Nov 22, 2015
    Messages:
    14
    Likes Received:
    4
    Reading List:
    Link
    Last edited: Jan 21, 2016
    LordCorwin likes this.
  11. Fordask

    Fordask I feel like having a Bacon Pizza

    Joined:
    Nov 12, 2015
    Messages:
    159
    Likes Received:
    201
    Reading List:
    Link
    The problem here is that
    "chapterContentWapper" needs to be replaced with "novel_honbun" as well. And they use single linebreack instead of double. Blame the baddly designed script for that. I should have just replaced <br> with <div></div> on the entire page and this wouldnt have been a problem

    This should work:
    Code:
    // ==UserScript==
    // @name         syosetu-google translate fix
    // @version      0.1
    // @description  Wrap text bocks on 17k in p tags so google translate wont skrew up the format
    // @author       Me
    // @match        http://*.syosetu.com/*
    // @grant        none
    // ==/UserScript==
    /* jshint -W097 */
    'use strict';
    
    // Your code here...
    var orignalstring = document.getElementById("novel_honbun").innerHTML;
    var newstring = "<p>" + orignalstring.replace(new RegExp("<br>", "g"), "</p><br><p>") + "</p>";
    document.getElementById("novel_honbun").innerHTML = newstring;
    
    Ill fix it to make it more flexible in a min
     
    Last edited: Jan 22, 2016
    blee8, LordCorwin and schnitter like this.
  12. valdesaga

    valdesaga [IDN] [7thMay] [V]aldes

    Joined:
    Oct 20, 2015
    Messages:
    852
    Likes Received:
    479
    Reading List:
    Link
    MASTER !

    PLEASE MAKE ME YOUR DISCIPLINE !!
     
  13. Cabman11

    Cabman11 Well-Known Member

    Joined:
    Nov 29, 2015
    Messages:
    666
    Likes Received:
    499
    Reading List:
    Link
    A lot of us read on mobile so how do you fix that?
     
  14. ReaderBot

    ReaderBot Android R-18 Series Winter Edition

    Joined:
    Nov 5, 2015
    Messages:
    333
    Likes Received:
    229
    Reading List:
    Link
    There is tampermonkey app on playstore, maybe you could try that.
     
  15. mllhild

    mllhild Active Member

    Joined:
    Nov 22, 2015
    Messages:
    14
    Likes Received:
    4
    Reading List:
    Link
    Thanks, it did work and the translation is better than bing.
     
  16. Fordask

    Fordask I feel like having a Bacon Pizza

    Joined:
    Nov 12, 2015
    Messages:
    159
    Likes Received:
    201
    Reading List:
    Link
    This should be easier to edit. Just change the @match (I wouldnt match all sites since it would slow down your browsing experience). It wont work on all sights but it should work on quit a few.
    Code:
    // ==UserScript==
    // @name         sitename-google translate fix
    // @version      0.1
    // @description  Replace line breaks with divs so google translate wont screw up the format
    // @author       Me
    // @match        http://*.sitename.com/*
    // @grant        none
    // ==/UserScript==
    /* jshint -W097 */
    'use strict';
    
    // Your code here...
    var orignalstring = document.getElementsByTagName('body')[0].innerHTML;
    var newstring = orignalstring.replace(new RegExp("<br>", "g"), '<div style="margin-bottom:1rem;"></div>');
    document.getElementsByTagName("body")[0].innerHTML = newstring;
    
    You could use greesemonkey and firefox. Unfortunately mobile chrome doesn't support extensions.
     
    Last edited: Jan 22, 2016
    blee8 and LordCorwin like this.
  17. Shio

    Shio Moderator Staff Member

    Joined:
    Oct 21, 2015
    Messages:
    6,059
    Likes Received:
    12,344
    Reading List:
    Link
    I'll place this in tutorial section, so more people could see this and it won't get buried by other thread.
     
  18. Sansome

    Sansome A Kind and Honest Villager X

    Joined:
    Oct 21, 2015
    Messages:
    428
    Likes Received:
    299
    Reading List:
    Link
    That's... a big help.. Thank you!! and now one of few my problem IRL is vanished like a thin air.. I give you my big Thumb!:)
     
  19. Em_Rez

    Em_Rez Well-Known Member

    Joined:
    Dec 30, 2015
    Messages:
    398
    Likes Received:
    446
    Reading List:
    Link
    Wow, awesome :)

    thanks for making this
     
  20. tocos10

    tocos10 Even more then the greatest Poster

    Joined:
    Oct 20, 2015
    Messages:
    2,467
    Likes Received:
    2,733
    Reading List:
    Link
Thread Status:
Not open for further replies.