网站弹广告是什么样做的,温州网站设计网站建设网站,阿里云做网站电话,wordpress 主题js一、覆盖默认右键菜单的方式 出于安全和用户体验的考虑#xff0c;不允许直接修改或向默认的右键菜单#xff08;浏览器的上下文菜单#xff09;添加自定义项。因为允许网页脚本更改或扩展默认的上下文菜单可能会造成用户困惑#xff0c;并有可能被恶意网站利用来进行钓鱼或…一、覆盖默认右键菜单的方式 出于安全和用户体验的考虑不允许直接修改或向默认的右键菜单浏览器的上下文菜单添加自定义项。因为允许网页脚本更改或扩展默认的上下文菜单可能会造成用户困惑并有可能被恶意网站利用来进行钓鱼或其他不良行为。所以一般的右键菜单都是自定义的会在监听事件中添加阻止默认事件event.preventDefault()。
!DOCTYPE html
html langen
head
meta charsetUTF-8
meta nameviewport contentwidthdevice-width, initial-scale1.0
titleCustom Context Menu/title
style/* 样式自定义右键菜单 */.custom-context-menu {display: none;position: absolute;z-index: 10;background-color: #ffffff;border: 1px solid #ddd;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);}.custom-context-menu ul {list-style: none;margin: 0;padding: 10px 0;}.custom-context-menu ul li {padding: 8px 16px;cursor: pointer;}.custom-context-menu ul li:hover {background-color: #f7f7f7;}
/style
/head
body
div idcontext-areaRight click me!/divdiv classcustom-context-menu idcustomContextMenuulliCustom Action 1/liliCustom Action 2/liliCustom Action 3/li/ul
/divscriptconst contextArea document.getElementById(context-area);const customContextMenu document.getElementById(customContextMenu);contextArea.addEventListener(contextmenu, function(event) {event.preventDefault(); // 阻止默认的右键菜单// 设置自定义菜单的位置customContextMenu.style.top ${event.clientY}px;customContextMenu.style.left ${event.clientX}px;// 显示自定义菜单customContextMenu.style.display block;// 点击自定义菜单的事件监听customContextMenu.addEventListener(click, function(e) {// 这里处理你的自定义菜单项被点击的逻辑// e.target 可以获取到被点击的元素console.log(Custom menu item clicked, e.target.textContent);});// 点击其他地方隐藏自定义菜单document.addEventListener(click, function(event) {if (event.button ! 2) { // 只在不是鼠标右键的情况下隐藏菜单customContextMenu.style.display none;}}, { once: true }); // 使用{ once: true }这个事件将在触发一次后被自动移除});
/script
/body
/html
二、使用插件的方式在默认右键菜单添加自定义事件 如果真的需要在默认右键菜单中添加自定义事件可以通过浏览器扩展程序来实现这些扩展程序可以使用特殊的API来向浏览器的上下文菜单中添加自定义项。这样做需要用户明确安装这个扩展程序并授予它修改上下文菜单的权限。扩展程序有更多的权限来与浏览器的一些内置功能交互包括修改上下文菜单。官网参考例子https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/api-samples/contextMenus/basic/sample.js
1. 首先需要在 manifest中添加对应的 contextMenus 权限
{name: 自定义右键菜单项,description: Uses the chrome.contextMenus API to customize the context menu.,version: 0.7,permissions: [contextMenus],background: {service_worker: background.js},content_scripts: [{matches: [all_urls],js: [content_script.js]}],manifest_version: 3
}
2. 然后background.js中添加监听
// 监听菜单项点击事件
chrome.contextMenus.onClicked.addListener((info, tab) {// 可以通过 sendMessage 将信息发送到content_script.js处理 或 在此处处理chrome.tabs.sendMessage(tab.id, { message: Custom ContextMenus Action, menuItemId: info.menuItemId, selectedText: info.selectionText});
});chrome.runtime.onInstalled.addListener(function() {chrome.contextMenus.create({id: zh_translate,title: 翻译成中文,contexts: [selection] // all 右键菜单都会显示page 仅页面菜单会显示selection 仅选中文本右键菜单显示link 链接右键菜单显示image 图片右键菜单显示audio 音频video视频editable编辑});chrome.contextMenus.create({id: en_translate,title: 翻译成英文,contexts: [selection]});
});
3. 在content_script.js接收数据
chrome.runtime.onMessage.addListener((request, sender, sendResponse) {if (request.message Custom ContextMenus Action) {console.log(request)}
});