![如何在 Firefox OS 模擬器中長按滑鼠後避免出現 ContextMenu](https://rvso.com/image/1465813/%E5%A6%82%E4%BD%95%E5%9C%A8%20Firefox%20OS%20%E6%A8%A1%E6%93%AC%E5%99%A8%E4%B8%AD%E9%95%B7%E6%8C%89%E6%BB%91%E9%BC%A0%E5%BE%8C%E9%81%BF%E5%85%8D%E5%87%BA%E7%8F%BE%20ContextMenu.png)
我在 Firefox OS Simulator 中安裝了一個聊天應用程式。
該應用程式添加了contextmenu
事件監聽器,它允許我刪除單個訊息。
但是,該事件偵聽器阻止我選擇訊息中的文字。
我不關心這個問題在觸控設備中是如何解決的。由於我使用的是 PC,因此我可以contextmenu
透過點擊滑鼠右鍵來調度事件偵聽器。
因此,當我按住滑鼠左鍵時,我希望該contextmenu
事件變為紅色,但仍然能夠選擇文字。
答案1
這是每個應用程式的解決方案:
找到應用程式的路徑。會是這樣的
[Firefox profile]\extensions\[Firefox OS Simulator]\profile\webapps\[ID]\application.zip
備份並解壓縮
找到新增事件監聽器的JS檔
contextmenu
。可能會是這樣的
someThing.addEventListener('contextmenu', function handler(event) { // ... });
過濾掉滑鼠左鍵:
someThing.addEventListener('contextmenu', function handler(event) { if(evt.button === 0) return; // ... });
確保模擬器已關閉
- 將修改後的文件插入到
application.zip
.
答案2
此行為不是錯誤,而是作為一項功能:
模擬桌面上的觸控事件
如果您仍想停用它,請按照下列步驟操作:
- 開啟Firefox的設定檔資料夾:
- 去
about:support
- 找到“應用程式基礎知識”部分
- 在表中找到「設定檔資料夾」條目
- 點選“顯示資料夾”按鈕
- 去
- 轉到
extensions
子資料夾 - 找到你的模擬器的資料夾,例如
[email protected]
- 轉到
b2g/modules/devtools
子資料夾 touch-events.js
使用適當的文字編輯器開啟文件求
sendContextMenu
函數:sendContextMenu: function teh_sendContextMenu(target, x, y, delay) { let doc = target.ownerDocument; let evt = doc.createEvent('MouseEvent'); evt.initMouseEvent('contextmenu', true, true, doc.defaultView, 0, x, y, x, y, false, false, false, false, 0, null); let content = this.getContent(target); let timeout = content.setTimeout((function contextMenu() { target.dispatchEvent(evt); this.cancelClick = true; }).bind(this), delay); return timeout; },
註解此行以避免調度事件:
// target.dispatchEvent(evt);
重新啟動模擬器
請注意,僅阻止事件的調度而不是不呼叫非常重要sendContextMenu
。否則,按一下將不會被取消 ( this.cancelClick = true
),因此文字選取將無法正常運作。