<commit_before>/* Copyright (c) 2013 The Tagspaces Authors. All rights reserved.
 * Use of this source code is governed by a AGPL3 license that 
 * can be found in the LICENSE file. */

define(function(require, exports, module) {
"use strict";

    console.log("Loading editorHTML");

    var extensionTitle = "HTML Editor";
    var extensionID = "editorHTML";  // ID should be equal to the directory name where the ext. is located
    var extensionType =  "editor";
    var extensionIcon = "icon-list";
    var extensionVersion = "1.0";
    var extensionManifestVersion = 1;
    var extensionLicense = "AGPL";
    var extensionSupportedFileTypes = [ "html", "htm" ];

    var TSCORE = require("tscore");

    var extensionsPath = TSCORE.Config.getExtensionPath();

    var extensionDirectory = extensionsPath+"/"+extensionID;

    var currentContent,
        currentFilePath,
        $containerElement;

    exports.init = function(filePath, containerElementID) {
        console.log("Initalization HTML Editor...");

        $containerElement = $('#'+containerElementID);

        currentFilePath = filePath;

        //var fileExt = TSCORE.TagUtils.extractFileExtension(filePath);

        $containerElement.empty();
        $containerElement.css("background-color","white");

        var extPath = extensionDirectory+"/index.html";
        $containerElement.append($('<iframe>', {
            id: "iframeViewer",
            sandbox: "allow-same-origin allow-scripts",
            scrolling: "no",
            style: "background-color: white; overflow: hidden;",
            src: extPath+"?cp="+filePath,
            "nwdisable": "",
            "nwfaketop": ""
        }));
        TSCORE.IO.loadTextFile(filePath);

//        if (!window.addEventListener) {
//            window.attachEvent('onmessage', function(e) {alert(e.origin);alert(e.data);});
//        } else {
//            window.addEventListener('message', function(e) {alert(e.origin);alert(e.data);}, false);
//        }
    };

    exports.setFileType = function(fileType) {
        console.log("setFileType not supported on this extension");
    };

    exports.viewerMode = function(isViewerMode) {
        // set readonly
        console.log("viewerMode not supported on this extension");
    };

    exports.setContent = function(content) {
        currentContent = content;

        var bodyRegex = /\<body[^>]*\>([^]*)\<\/body/m;
        var bodyContent = undefined;

        try {
            bodyContent = content.match( bodyRegex )[1];
        } catch(e) {
            console.log("Error parsing HTML document. "+e);
            TSCORE.FileOpener.closeFile(true);
            TSCORE.showAlertDialog("Probably a body tag was not found in the document. Document will be closed.","Error parsing HTML document");
        }

//        var titleRegex = /\<title[^>]*\>([^]*)\<\/title/m;
//        var titleContent = content.match( titleRegex )[1];

        // removing all scripts from the document
        var cleanedBodyContent = bodyContent.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,"");

        var contentWindow = document.getElementById("iframeViewer").contentWindow;
        if(typeof contentWindow.setContent === "function") {
            contentWindow.setContent(cleanedBodyContent);
        } else {
            // TODO optimize setTimeout
            window.setTimeout(function() {
                contentWindow.setContent(cleanedBodyContent);
            }, 500);
        }
    };

    var contentVersion = 0;

    function resetContentVersion() {
        contentVersion = 0;
        document.getElementById("iframeViewer").contentWindow.resetContentVersion();
    }

    function checkContentChanged() {
        var newContentVersion;
        //try {
            newContentVersion = document.getElementById("iframeViewer").contentWindow.getContentVersion();
        //} catch(e) { }
        if(newContentVersion > contentVersion) {
            contentVersion = newContentVersion;
            TSCORE.FileOpener.setFileChanged(true);
            // autosave
            //TSCORE.FileOpener.saveFile();
        }
        window.setTimeout(checkContentChanged, 1000);
    }

    window.setTimeout(checkContentChanged, 1000);

    exports.getContent = function() {
        var content = $("#iframeViewer").contents().find(".note-editable").html();

        // removing all scripts from the document
        var cleanedContent = content.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,"");

        // saving all images as png in base64 format
        var match,
            urls = [],
            imgUrl = "",
            rex = /<img.*?src="([^">]*\/([^">]*?))".*?>/g;;
<commit_msg>fixing issue with html editor, not reacting on document change<commit_after>/* Copyright (c) 2013 The Tagspaces Authors. All rights reserved.
 * Use of this source code is governed by a AGPL3 license that 
 * can be found in the LICENSE file. */

define(function(require, exports, module) {
"use strict";

    console.log("Loading editorHTML");

    var extensionTitle = "HTML Editor";
    var extensionID = "editorHTML";  // ID should be equal to the directory name where the ext. is located
    var extensionType =  "editor";
    var extensionIcon = "icon-list";
    var extensionVersion = "1.0";
    var extensionManifestVersion = 1;
    var extensionLicense = "AGPL";
    var extensionSupportedFileTypes = [ "html", "htm" ];

    var TSCORE = require("tscore");

    var extensionsPath = TSCORE.Config.getExtensionPath();

    var extensionDirectory = extensionsPath+"/"+extensionID;

    var currentContent,
        currentFilePath,
        $containerElement;

    exports.init = function(filePath, containerElementID) {
        console.log("Initalization HTML Editor...");

        $containerElement = $('#'+containerElementID);

        currentFilePath = filePath;

        //var fileExt = TSCORE.TagUtils.extractFileExtension(filePath);

        $containerElement.empty();
        $containerElement.css("background-color","white");

        var extPath = extensionDirectory+"/index.html";
        $containerElement.append($('<iframe>', {
            id: "iframeViewer",
            sandbox: "allow-same-origin allow-scripts",
            scrolling: "no",
            style: "background-color: white; overflow: hidden;",
            src: extPath+"?cp="+filePath,
            "nwdisable": "",
            "nwfaketop": ""
        }));
        TSCORE.IO.loadTextFile(filePath);

//        if (!window.addEventListener) {
//            window.attachEvent('onmessage', function(e) {alert(e.origin);alert(e.data);});
//        } else {
//            window.addEventListener('message', function(e) {alert(e.origin);alert(e.data);}, false);
//        }
    };

    exports.setFileType = function(fileType) {
        console.log("setFileType not supported on this extension");
    };

    exports.viewerMode = function(isViewerMode) {
        // set readonly
        console.log("viewerMode not supported on this extension");
    };

    exports.setContent = function(content) {
        currentContent = content;

        var bodyRegex = /\<body[^>]*\>([^]*)\<\/body/m;
        var bodyContent = undefined;

        try {
            bodyContent = content.match( bodyRegex )[1];
        } catch(e) {
            console.log("Error parsing HTML document. "+e);
            TSCORE.FileOpener.closeFile(true);
            TSCORE.showAlertDialog("Probably a body tag was not found in the document. Document will be closed.","Error parsing HTML document");
        }

//        var titleRegex = /\<title[^>]*\>([^]*)\<\/title/m;
//        var titleContent = content.match( titleRegex )[1];

        // removing all scripts from the document
        var cleanedBodyContent = bodyContent.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,"");

        var contentWindow = document.getElementById("iframeViewer").contentWindow;
        if(typeof contentWindow.setContent === "function") {
            contentWindow.setContent(cleanedBodyContent);
        } else {
            // TODO optimize setTimeout
            window.setTimeout(function() {
                contentWindow.setContent(cleanedBodyContent);
            }, 500);
        }
    };

    var contentVersion = 0;

    function resetContentVersion() {
        contentVersion = 0;
        document.getElementById("iframeViewer").contentWindow.resetContentVersion();
    }

    function checkContentChanged() {
        var newContentVersion;
        try {
            newContentVersion = document.getElementById("iframeViewer").contentWindow.getContentVersion();
        } catch(e) { }
        if(newContentVersion > contentVersion) {
            contentVersion = newContentVersion;
            TSCORE.FileOpener.setFileChanged(true);
            // autosave
            //TSCORE.FileOpener.saveFile();
        }
        window.setTimeout(checkContentChanged, 1000);
    }

    window.setTimeout(checkContentChanged, 1000);

    exports.getContent = function() {
        var content = $("#iframeViewer").contents().find(".note-editable").html();

        // removing all scripts from the document
        var cleanedContent = content.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,"");

        // saving all images as png in base64 format
        var match,
            urls = [],
            imgUrl = "",
            rex = /<img.*?src="([^">]*\/([^">]*?))".*?>/g;;

{'fraction_non_alphanumeric': 0.14264534575876311, 'fraction_numerical': 0.005294927459493805, 'mean_word_length': 2.5212527964205815, 'pattern_counts': {'":': 4, '<': 39, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5264238', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2505, 'n_words': 563}