View Single Post
Old 01-20-2016, 07:53 AM   #193
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,737
Karma: 24031401
Join Date: Dec 2010
Device: Kindle PW2
JavaScript in the Preview window console

I've found out by chance that it's possible to run JavaScript code in the Preview console window.
Of course, since the read-only Preview window doesn't allow edits, you can only use it for temporary changes. For example, you can highlight certain words.

You can test this with the following simple script, which will highlight all search terms. (BTW, only the first line is by me.)

Spoiler:
Code:
// Highlight text example
enteredText = prompt("What do want to search for?");

findAndReplace('(' + enteredText + ')', '<span style="background-color: #FFFF00;">$1</span>')

// code by James Padolsey; http://james.padolsey.com/javascript/find-and-replace-text-with-javascript/
function findAndReplace(searchText, replacement, searchNode) {
    if (!searchText || typeof replacement === 'undefined') {
        // Throw error here if you want...
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}


(To display the console window, click Console in the grey menu bar below the Preview window; press Enter to run the code.)

EDIT: This script doesn't work with all ePubs. With some ePubs you might get the following error:

Code:
Error: SyntaxError: DOM Exception 12
and I haven't got the faintest idea what's causing this.

If it doesn't work with your book, you can test it with Sigil_python_plugins.html from the Plugin Framework guide.

Last edited by Doitsu; 01-09-2018 at 02:41 AM.
Doitsu is offline   Reply With Quote