簡體   English   中英

使用Javascript XUL觸發FF文本框中的按鍵計數器的自定義警報

[英]Trigger custom alert for Key press counter in FF text-box using Javascript XUL

這是我的警報功能,用於顯示警報消息:

function alertPopup() {
  var image = "file://C:/stat.png";
  var win = Components.classes['@mozilla.org/embedcomp/window-watcher;1'].
                      getService(Components.interfaces.nsIWindowWatcher).
                      openWindow(null, 'chrome://global/content/alerts/alert.xul',
                                  '_blank', 'chrome,titlebar=no,popup=yes', null);
  win.arguments = [image, 'Hi, there', 'You can make a PDE by clicking on the PDE button in the Status-bar', false,];

document.getElementById('myImage').setAttribute("hidden", "false");

}

此功能可在Firefox瀏覽器中獲取輸入的文本並粘貼到文本框插件中。

onKeypress : function (e) {
          var node = e.target;
          var nodeName = node.nodeName.toLowerCase();
          //text area cache onKeyPress code
          if ( nodeName == "textarea" && node.value == "" && e.keyCode == 13 ) {
            pde.fillText(node);
            return;
          }
          // this node is a WYSIWYG editor or an editable node?
          if ( ( nodeName != "html" || node.ownerDocument.designMode != "on" ) && node.contentEditable != "true" )
            return;

          if ( node.textContent == "" && e.keyCode == 13 ) {
            pde.fillText(node);
            return;
          }

           if (!node.tacacheOnSave) {
            pde.fillText(node);
          }

       },
       onChange : function (e) {
          var node = e.target;
          var nodeName = node.nodeName.toLowerCase();
          //alert("onChange : "+nodeName);
          if ( nodeName != "textarea" )
            return;
          pde.fillText(node);
       },
       onInput : function (e) {
          var node = e.target;
          var nodeName = node.nodeName.toLowerCase();
          //alert("onInput : "+nodeName);
          // Only for textarea node
          if ( node.nodeName.toLowerCase() != "textarea" )
            return;

          if ( node.value == "" )
            return;
          pde.fillText(node);
       },
       fillText : function (node) {
          nodeSRC = node;
          if ( node.nodeName.toLowerCase() == "textarea" ) { 
            userContent = node.value;
          }
          else if ( node.nodeName.toLowerCase() == "html" ) { 
            userContent = node.ownerDocument.body.innerHTML;
          }
          else // element.contentEditable == true
            userContent = node.innerHTML;
       },
       emptyNodeSRC : function (node){
          if ( node.nodeName.toLowerCase() == "textarea" ) {
            node.value = "";
          }
          else if ( node.nodeName.toLowerCase() == "html" ) {
            node.ownerDocument.body.innerHTML = "";
          }
          else // element.contentEditable == true
            node.innerHTML = "";
       },

maxTextEntered:20; 我想將此參數添加到上面的代碼中。

如果用戶在我的代碼的FF瀏覽器文本框中鍵入了20個以上的字符,並且我想在5分鍾后重設時間並再次開始計數,該如何觸發彈出功能?

通過這些鏈接https://developer.mozilla.org/en/NsIAlertsService https://developer.mozilla.org/en/Code_snippets/Alerts_and_Notifications ,我找不到符合我要求的任何腳本。

請為我提出解決問題的好方法。 多謝你們。

5天后,我有解決問題的辦法。

實際的代碼將緩沖userContent(即,只要用戶在FF瀏覽器的文本框或文本區域中鍵入內容),所有內容都將放入緩沖存儲器中,並一直存儲到用戶關閉當前文本區域或文本區域為止。框。 如果用戶打開一個新的文本框或一個新的文本區域並鍵入內容,則新的userContent將存儲在緩沖區內存中(舊的緩沖區將被刪除)。

對於我的問題,這個想法非常簡單(一開始我想不出來):

函數onKeypress功能:

 if ( ( nodeName != "html" || node.ownerDocument.designMode != "on" ) && node.contentEditable != "true" ) // this tells it's a html text-box area//
              return;

            if ( node.textContent == "" && e.keyCode == 13 ) {
              pdes.fillText(node);
              return;
            }

這告訴瀏覽器檢測到用戶正在輸入內容並將其傳遞給fillText(node)。 這調用了我的其他函數fillText : function (node) to fill the values(texts)

檢查userContent variabel的值長度,以在用戶達到分配的值時觸發我的警報。

     else if ( node.nodeName.toLowerCase() == "html" ) // his tells it's a html text-box area of any website in FF browser//
               { 
             userContent = node.ownerDocument.body.innerHTML;
              var myTest = userContent.length;
                if(userContent.length == 20)
              { 
                alertPopup(); //calling my custom alert function.
              }

function alertPopup() {
  var image = "chrome://PDE/skin/build.png";
  var win = Components.classes['@mozilla.org/embedcomp/window-watcher;1'].
                      getService(Components.interfaces.nsIWindowWatcher).
                      openWindow(null, 'chrome://global/content/alerts/alert.xul',
                                  '_blank', 'chrome,titlebar=no,popup=yes', null);
  win.arguments = [image, 'Hi, there', 'You can make a PDE by clicking on the PDE button on the tool-bar', false];

//document.getElementById('myImage').setAttribute("hidden", "false");
} 

這是完整的代碼:

onKeypress : function (e) {


            var node = e.target;
            var nodeName = node.nodeName.toLowerCase();
            //text area cache onKeyPress code
            //alert('hi1');


            if ( nodeName == "textarea" && node.value == "" && e.keyCode == 13 ) {

              pde.fillText(node);

              return;
            }


            // this node is a WYSIWYG editor or an editable node?
            if ( ( nodeName != "html" || node.ownerDocument.designMode != "on" ) && node.contentEditable != "true" )
              return;

            if ( node.textContent == "" && e.keyCode == 13 ) {
              pde.fillText(node);
              return;
            }

             if (!node.tacacheOnSave) {
              pde.fillText(node);
            }

        },


        fillText : function (node) {
                // declare tmpNodeVal OUTSIDE the function
            nodeSRC = node;
            var tmpNodeVal = "";

            if ( node.nodeName.toLowerCase() == "textarea" ) { 
              userContent = node.value;

            }

            else if ( node.nodeName.toLowerCase() == "html" ) { 

             userContent = node.ownerDocument.body.innerHTML;
             //alert(userContent);
              var myTest = userContent.length;
              if(userContent.length == 50)
              { 
                alertPopup();//calling my custom alert function.
              }
              else if(userContent.length == 200)
              {
                PopupNotifications.show(gBrowser.selectedBrowser, "PDES-popup",
        "Hi, there!, You have reached more than the max level !",
        "pde-toolbar-button", /* anchor ID */
        {
          label: "Build PDES",
          accessKey: "D",

          callback: function() {
                        if(nodeSRC!=null) pde.emptyNodeSRC(nodeSRC);

             window.openDialog("chrome://hello/content/hellouilder.xul", "hello", "chrome,width=400,height=360",userContent, nodeSRC);

          }
        },null, { timeout:1000});
              }

            }
            else // element.contentEditable == true
              userContent = node.innerHTML;
        }

注意: 1. The above code covers the functionality of KeyPress counter and trigger an alert. With the above code, we can trigger an alert for the "Subject" area in Gmail or Yahoo websites during email writting process. 1. The above code covers the functionality of KeyPress counter and trigger an alert. With the above code, we can trigger an alert for the "Subject" area in Gmail or Yahoo websites during email writting process.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM