<i id='HUfqS'><tr id='HUfqS'><dt id='HUfqS'><q id='HUfqS'><span id='HUfqS'><b id='HUfqS'><form id='HUfqS'><ins id='HUfqS'></ins><ul id='HUfqS'></ul><sub id='HUfqS'></sub></form><legend id='HUfqS'></legend><bdo id='HUfqS'><pre id='HUfqS'><center id='HUfqS'></center></pre></bdo></b><th id='HUfqS'></th></span></q></dt></tr></i><div id='HUfqS'><tfoot id='HUfqS'></tfoot><dl id='HUfqS'><fieldset id='HUfqS'></fieldset></dl></div>

        <bdo id='HUfqS'></bdo><ul id='HUfqS'></ul>

    1. <tfoot id='HUfqS'></tfoot>

      <small id='HUfqS'></small><noframes id='HUfqS'>

    2. <legend id='HUfqS'><style id='HUfqS'><dir id='HUfqS'><q id='HUfqS'></q></dir></style></legend>
    3. Vanilla JS:完全禁用“保存";网页中的功能

      Vanilla JS: Totally disabling a quot;savequot; functionality in webpages(Vanilla JS:完全禁用“保存;网页中的功能)

      1. <i id='QOmJ3'><tr id='QOmJ3'><dt id='QOmJ3'><q id='QOmJ3'><span id='QOmJ3'><b id='QOmJ3'><form id='QOmJ3'><ins id='QOmJ3'></ins><ul id='QOmJ3'></ul><sub id='QOmJ3'></sub></form><legend id='QOmJ3'></legend><bdo id='QOmJ3'><pre id='QOmJ3'><center id='QOmJ3'></center></pre></bdo></b><th id='QOmJ3'></th></span></q></dt></tr></i><div id='QOmJ3'><tfoot id='QOmJ3'></tfoot><dl id='QOmJ3'><fieldset id='QOmJ3'></fieldset></dl></div>
          <tbody id='QOmJ3'></tbody>
          • <bdo id='QOmJ3'></bdo><ul id='QOmJ3'></ul>

            1. <tfoot id='QOmJ3'></tfoot>
            2. <legend id='QOmJ3'><style id='QOmJ3'><dir id='QOmJ3'><q id='QOmJ3'></q></dir></style></legend>
              • <small id='QOmJ3'></small><noframes id='QOmJ3'>

                本文介绍了Vanilla JS:完全禁用“保存";网页中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我是一名初级 Wikipedia 用户,只想尝试在编辑页面中使用 Wiki 文本编辑器更改一些 Wikipedian 内容,但不以任何方式保存我的更改(即使是错误的),因此寻求防止任何在编辑页面中保存功能的方法,通过 vanilla JavaScript.

                Assume I'm a junior Wikipedia user that just want to experiment with changing some wikipedian content with the Wiki text editor in an edit-page, but not saving my changes in any way (not even by mistake), thus seeking a way to prevent any saving functionality in an edit-page, via vanilla JavaScript.

                如果我去一些 edit-page 在希伯来语维基百科中我可以通过单击保存页面"按钮(插图)来保存或发布页面,其中我可以从 DOM 中删除:

                If I go for some edit-page in Hebrew Wikipedia I can save or publish a page by mouse-clinking the Save page button (illustration), which I can remove from DOM with:

                document.querySelector("#wpSave").remove();
                

                <小时>

                但假设我仍然可以通过 alt+shift+s 保存或发布内容,并且我也想防止这种可能的保存行为;我为此尝试了以下代码:


                But let's assume I can still save or publish content by alt+shift+s and I'd like to prevent this possible saving behavior as well; I tried the following code for that:

                // ==UserScript==
                // @name         wiki
                // @match        https://*.wikipedia.org/*
                // ==/UserScript==
                
                document.addEventListener("DOMContentLoaded", ()=>{
                    document.addEventListener('keypress', function(e) {
                        if (e.key == 16 && e.key == 18 && e.key == 83) {
                            return false;
                        }
                    });
                });
                

                代码失败(控制台中没有给出特殊错误).为什么失败了?

                The code failed (no special error is given in console). Why it failed?

                推荐答案

                你的问题有很多问题:

                • event.keyevent.keyCode 不同,参考 文档.
                • e.key == 16 &&e.key == 18 &&e.key == 83 永远不会是真的.
                • 从事件侦听器返回 false 不会阻止事件传播.
                • event.key isn't the same as event.keyCode, Refer to the documentation.
                • e.key == 16 && e.key == 18 && e.key == 83 will never be true.
                • Returning false from an event listener doesn't stop the event from being propagated.

                您正在尝试做的事情可以通过以下方式实现:

                What you are trying to do can be achieved in the following way:

                document.addEventListener("keypress", evt => {
                  // refer to https://stackoverflow.com/a/2878005/8746648
                  if(evt.altKey && evt.key == "S") {
                    alert("prevent this message");
                    evt.preventDefault();
                  }
                });
                
                // refer to https://stackoverflow.com/a/35611393/8746648
                document.addEventListener("keypress", evt => {
                  if(evt.altKey && evt.key == "S") {
                    evt.stopPropagation();
                  }
                }, true);

                1. 注意第二个事件监听器中的 true.
                2. 请注意,evt.key 与大写s"进行比较.
                3. 如果事件侦听器已在捕获阶段注册,则无法阻止它运行.(在这里了解捕获和blobbing阶段).
                1. Notice the true in the second event listener.
                2. Notice that evt.key is compared with an upper case "s".
                3. You can't prevent an event listener from running if it's registered in the capturing phase. (read about the capture and blobbing phases here).

                这篇关于Vanilla JS:完全禁用“保存";网页中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                      <bdo id='JTvsS'></bdo><ul id='JTvsS'></ul>

                          <tbody id='JTvsS'></tbody>
                        <tfoot id='JTvsS'></tfoot>
                        <legend id='JTvsS'><style id='JTvsS'><dir id='JTvsS'><q id='JTvsS'></q></dir></style></legend>
                      1. <i id='JTvsS'><tr id='JTvsS'><dt id='JTvsS'><q id='JTvsS'><span id='JTvsS'><b id='JTvsS'><form id='JTvsS'><ins id='JTvsS'></ins><ul id='JTvsS'></ul><sub id='JTvsS'></sub></form><legend id='JTvsS'></legend><bdo id='JTvsS'><pre id='JTvsS'><center id='JTvsS'></center></pre></bdo></b><th id='JTvsS'></th></span></q></dt></tr></i><div id='JTvsS'><tfoot id='JTvsS'></tfoot><dl id='JTvsS'><fieldset id='JTvsS'></fieldset></dl></div>

                          <small id='JTvsS'></small><noframes id='JTvsS'>