本文介绍了如何在Chrome的Kiosk模式下右键点击/长时间触摸禁用上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在研究一个博物馆的软件。有几个交互式信息亭在Windows 8.1上运行,并连接到本地网络。没有键盘,没有鼠标。带有Apache的服务器包含多个本地网站。每个自助服务终端在Kiosk模式下运行Google Chrome的副本。所以,我们有一些本地的网络应用程序,为博物馆的访问者提供信息。

现在,问题出现了。如果访问者在屏幕上长时间触摸,它就像右键单击一样。出现上下文菜单。我们根本不需要它。我已经将oncontextmenu = return false添加到body标签中,并且它有所帮助。但。我们有几个运行在iframe中的外部网站(博物馆与互联网有联系)。并且上下文菜单不会出现在iframe上。 AFAIK,无法使用JavaScript禁用它。



我们的系统工程师获得了一个完全禁用Windows中的右键单击的软件。包括Chrome的任何地方但。它适用于鼠标。至于接触......好吧,它可以在除 Chrome之外的任何地方禁用触摸事件。也许Chrome有它自己的touch事件处理程序,我不知道。



毕竟。我们需要在Chrome的Kiosk模式中右键点击/长时间触摸,从而去除iframe上的上下文菜单。请给我一些建议。

解决方案

我假设您显示的是简单的 code>或 chrome-extension:// 页面或Google的网上应用店。如果您的自助服务终端显示应用,则整个策略将无法使用,因为此扩展程序无法访问其他应用或扩展程序内部的iframe(即使iframe的来源是其通常拥有权限的来源如果直接导航到 about:blank ,内容脚本将不会运行,并且可以显示上下文菜单。 (如果在iframe中加载了 about:blank ,但该块将正常工作。)

  • 如果iframe的 sandbox 属性不包含 allow-scripts 权限,那么扩展程序无法阻止该iframe中的上下文菜单。 li>


    只要这些限制都不适用(并且只要页面上的脚本本身不清除<$ c上的所有事件侦听器

    我已经使用上面的代码创建。 (开发者模式扩展现在会在启动时产生警告,而Web Store扩展则不会。)


    We are working on the software for a museum. There are several interactive kiosks with touch screen running on Windows 8.1 which are connected into local network. No keyboard, no mouse. The server with Apache on it contains several local websites. Each kiosk runs a copy of Google Chrome in a kiosk mode. So, we have some kind of local web applications which provide a museum visitor with information.

    Now, the problem. If a visitor does long touch on screen it works like an analogue of right click. Context menu appears. We don't want it at all. I've added "oncontextmenu = return false" into the body tag and it helped. But. We have a couple of external websites running in iframes (the museum has a connection to Internet). And context menu does appear on iframes. AFAIK, there is no way to disable it using javascript.

    Our system engineer got a software which completely disables right click in Windows. Anywhere including Chrome. But. It works for a mouse. And as for touches... well, it disables touch events anywhere besides Chrome. Maybe Chrome has its own touch events handler, I don't know.

    So, after all. We need to get rid off context menu on iframes on right click/long touch in a kiosk mode of Chrome. Please give me some advice.

    解决方案

    I assume you're displaying a plain http://... (or possibly https://... or file://...) Web page on your kiosk. If you're actually showing an app (i.e., chrome-extension://...), then this strategy will not work.

    A Chrome extension that injects window.addEventListener("contextmenu", function(e) { e.preventDefault(); }) into every browsing context would probably do the trick to block context menus on iframes.

    manifest.json:

    {
        "manifest_version": 2,
        "name": "Context Menu Blocker",
        "version": "1.0",
        "content_scripts": [
          {
            "matches": ["<all_urls>"],
            "js": ["contextblocker.js"],
            "all_frames": true,
            "match_about_blank": true
          }
        ]
    }
    

    contextblocker.js:

    window.addEventListener("contextmenu", function(e) { e.preventDefault(); })
    

    Simply create a folder and place the two files inside. Then, go to chrome://extensions/, check the Developer Mode box. Finally, click Load unpacked extension... and select the folder you just created.

    This should prevent the context menu from appearing in anywhere extension content scripts are allowed to run, include any page loaded inside of an iframe. There are few notable points where it fails:

    • Extensions are not allowed to run on chrome:// or chrome-extension:// pages, or on Google's Web Store. If your kiosk is displaying an app, this whole strategy won't work, because this extension won't be able to access iframes inside of another app or extension (even if the source of the iframe is an origin that it would normally have permission to access).
    • If you navigate directly to about:blank, the content script will not run and the context menu can appear. (If about:blank is loaded in an iframe, however, the block will work correctly.)
    • If an iframe has a sandbox attribute that does not include the allow-scripts permission, then the extension cannot block context menus from that iframe.

    As long as none of those restrictions apply (and as long as a script on the page itself does not clear all event listeners on window), then it should work.

    I have used the code above to create a simple extension in the Chrome Web Store. (Developer-mode extensions now produce a warning on start-up, while Web Store extensions do not.)

    这篇关于如何在Chrome的Kiosk模式下右键点击/长时间触摸禁用上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-21 10:15