eltomjan / ČT Teletext

// ==UserScript==
// @name         ČT Teletext
// @namespace    http://tampermonkey.net/
// @version      1
// @copyright    2021, El Tom Elzein
// @licence      MIT
// @description  CT TXT simplify 4 blind
// @match        https://www.ceskatelevize.cz/teletext/ct/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  function fixLinks(o, base) {
    var a = find(o, 'A');
    while (a) {
      var rest = a.href;
      rest = rest.substr(rest.indexOf('?'));
      //a.setAttribute("onclick", "location.href('" + base + rest + "')");
      //a.href = "#";
      a.href = base + rest;
      a = a.nextSibling;
      a = find(a, 'A');
    }
  }

  function find(i, TN) {
    while (i && i.tagName != TN) {
      if (i.firstElementChild) {
        i = i.firstElementChild;
      }
      else if (i.nextSibling) {
        i = i.nextSibling;
      }
      else {
        do {
          i = i.parentNode;
        } while (i && !i.nextSibling)
        if (!i) return;
        i = i.nextSibling;
      }
    }
    return i;
  }

  function swPage() {
    var i = document.getElementsByTagName("PRE")[0];
    if (!i) return;
    var htm = i.outerHTML;
    i = find(i, "P");
    var urlBase = url.substr(0, url.indexOf('?'));
    fixLinks(i, urlBase);
    var lnks = i.innerHTML;
    /* <span class='textctverd'>Stránka <strong>100 / 1</strong> &ndash; podstránky:
           <span id='teletextSubpages'>
               <a class='active' href='?page=100A&amp;ver=TXT'>1</a>
       */
    i = find(i, "FORM");
    if (!i) return;
    //i = i.firstElementChild.rows[0].cells[1];
    //fixLinks(i, urlBase);
    var f = i.outerHTML;
    f = f.replace(' ', ' onsubmit=fakeSubmit() ');
    lnks += f;
    /* <td class="center">
       <a class="sipka-vlevo" href="?page=899&amp;ver=TXT" id="goPrev" title="Stránka 899">Předchozí</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <a class="sipka-vpravo" href="?page=101&amp;ver=TXT" id="goNext" title="Stránka 101">Následující</a>
       <input type="hidden" name="ver" value="TXT" />*/
    document.body.innerHTML = htm + lnks;
  }
  var url = document.location.href;
  window.onload = swPage;
  swPage(url);
})();