うしブログ

うしブログ

趣味で運営する、GeoGebraの専門ブログ。

(作業メモ)StartPoint要検証(2行の場合;テキスト変更時未定義問題)

(要修復)ToggleButton・RollPolygonWithoutSlipping・貯金時計・直感力トレーニング

最前面のテキストエリアにエラーメッセージを表示させる実験

 

function ggbOnInit() {
    seizureErrorDialog(10);
}

// テキストエリアをhtmlに生成
document.body.insertAdjacentHTML('afterBegin', '<textarea id="usitext"></textarea>');
document.getElementById('usitext').style.position = "fixed";
document.getElementById('usitext').style.width = "400px";
document.getElementById('usitext').style.height = "85px";
document.getElementById('usitext').style.bottom = "10px";
document.getElementById('usitext').style.left = "30px";
document.getElementById('usitext').style.zIndex = "2147483647";
document.getElementById('usitext').style.backgroundColor = "#dcdcdc";
document.getElementById('usitext').style.outline = "none";
document.getElementById('usitext').style.resize = "none";

//テキストエリア要素を指定
var targetElement = document.getElementById('usitext');

//初期メッセージ
targetElement.value = "<エラーログ>";

//dragstartイベント不使用
targetElement.ondragstart = function (event) {
    return false;
};

// global
var offx;
var offy;

// ドラッグ・ドロップ時のイベント登録
targetElement.onmousedown = function (e) {
    offx = e.offsetX;
    offy = e.offsetY;
    targetElement.addEventListener("mousemove", onMouseMove);
};
var onMouseMove = function (event) {
    var x = event.clientX;
    var y = event.clientY;
    var width = targetElement.offsetWidth;
    var height = targetElement.offsetHeight;
    targetElement.style.removeProperty('bottom');
    targetElement.style.top = (y - offy) + "px";
    targetElement.style.left = (x - offx) + "px";
};
targetElement.onmouseup = function () {
    targetElement.removeEventListener("mousemove", onMouseMove);
};


//web版GeoGebraのエラーダイヤログを出ないようにして、エラーメッセージのみを横取りする
function seizureErrorDialog(maxRow) {

    // 対象ノード
    const target = document.getElementsByTagName("main")[0];

    // インスタンス
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            // console.log(mutation);
            if (mutation.addedNodes[0]) {
                if (mutation.addedNodes[0].className == "dialogComponent errorDialog") {
                    // console.log(mutation.addedNodes[0]);
                    var errText = mutation.addedNodes[0].getElementsByClassName("mainPanel")[0].textContent;
                    var outputText = "[GeoGebra - エラー]" + errText;
                    // console.log(outputText);
                    var currentLog = targetElement.value;
                    if (maxRow && currentLog.split("\n").length >= maxRow) {
                        var tempArr = currentLog.split("\n");
                        tempArr.shift();
                        currentLog = tempArr.join("\n");
                    }
                    targetElement.value = currentLog + "\n" + outputText;
                    mutation.addedNodes[0].getElementsByClassName("dialogContainedButton")[0].click();
                }
            }

        });
    });

    // オブザーバ
    const config = {
        childList: true,
        subtree: true
    };

    observer.observe(target, config);
}