使用 Javascript XUL 在 FF 文本框中触发按键计数器的自定义警报

发布于 2024-12-01 08:08:04 字数 3318 浏览 2 评论 0原文

这是我的警报函数,用于显示警报消息:

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 = "";
       },

最大文本输入数:20;我想将此参数添加到我上面的代码中。

如果用户在我的代码中的 FF 浏览器文本框中输入超过 20 个字符,并且我想在 5 分钟后重置时间并再次开始计数,如何触发弹出功能?

https://developer.mozilla.org/en/NsIAlertsService https://developer.mozilla.org/en/Code_snippets/Alerts_and_Notifications 来自这些链接,我找不到任何符合我要求的脚本。

请为我的问题提出好的解决方案。 谢谢你们。

This is my alert function to show the alert message:

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");

}

This funciton to to get the entered text in the Firefox browser and paste in the textbox plugin.

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; I want to add this parameter to my above code.

How do i trigger the pop-up function if the user typed more than 20 characetrs in the FF browser textbox in my code and I would like to reset the time after 5 mins and the start the counting once again?

https://developer.mozilla.org/en/NsIAlertsService
https://developer.mozilla.org/en/Code_snippets/Alerts_and_Notifications from these links, I couldn't find any script for my requirement.

Please propose me good solution to my problem.
Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2024-12-08 08:08:04

5天后,我的问题有了解决方案。

实际代码缓冲 userContent(即,当用户在 FF 浏览器文本框或文本区域中键入内容时)所有内容都将放入缓冲存储器中
&这将被存储,直到用户关闭当前的文本区域或文本框。
如果用户打开一个新的文本框或一个新的文本区域&输入一些内容,新的用户内容将存储在缓冲区内存中(旧缓冲区将被删除)。

对于我的问题来说,这个想法非常简单(我一开始无法深入思考):

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(节点)。这调用我的另一个函数
fillText:填充值(文本)的函数(节点)

检查 userContent 变量的值长度,以便在用户达到分配的数值时触发我的警报。

     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。上面的代码涵盖了按键计数器的功能并触发警报。
通过上面的代码,我们可以在电子邮件编写过程中触发 Gmail 或 Yahoo 网站中“主题”区域的警报。

After 5 days, I have a solution for my problem.

The actual code buffers the userContent (i.e when ever if the user types something in FF browser text-box or text area) everything will be put in the buffer memory
& this will be stored until the user closes the present text-area or text-box.
If the user opens up a new text-box or a new text-area & types something the new userContent will be stored in the buffer memeory(the old buffer will be deleted).

The idea is very simple for my problem(which i couldn't think deep in the beginning):

The function onKeypress function:

 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;
            }

This tells the browser to detect the user is typing something and pass it to the fillText(node). This call my other function
fillText : function (node) to fill the values(texts).

To check value length of the userContent variabel to trigger my alert if the user reached the assigned number value.

     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");
} 

Here is the full code:

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;
        }

Note:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文