mirror of
https://github.com/ION606/browser-chromium.git
synced 2026-06-06 00:07:06 +00:00
backup
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
|
||||
const findLink = (el) => {
|
||||
if (el.href) return el.href;
|
||||
else if (el.parentElement && el.parentElement !== document.body) return findLink(el.parentElement);
|
||||
else return null;
|
||||
}
|
||||
|
||||
|
||||
function showContextMenu(e) {
|
||||
document.querySelectorAll('.context-menu').forEach(el => el.remove());
|
||||
|
||||
const contextMenuActions = {
|
||||
'open in new tab': (url) => window.tabAPI.newTab(url),
|
||||
'test': () => console.log('test')
|
||||
}
|
||||
|
||||
// create the main context menu container
|
||||
const contextMenu = document.createElement('div');
|
||||
contextMenu.classList.add('context-menu');
|
||||
contextMenu.id = 'contextMenu';
|
||||
|
||||
// loop through menu options and create each item
|
||||
const l = findLink(e.target);
|
||||
|
||||
for (const key in contextMenuActions) {
|
||||
if (!l && key === 'open in new tab') continue;
|
||||
|
||||
const menuItem = document.createElement('div');
|
||||
menuItem.classList.add('context-menu-item');
|
||||
menuItem.textContent = key;
|
||||
|
||||
menuItem.onclick = (_) => {
|
||||
if (l && key === 'open in new tab') contextMenuActions[key](l);
|
||||
else contextMenuActions[key](e.target);
|
||||
}
|
||||
contextMenu.appendChild(menuItem);
|
||||
}
|
||||
|
||||
// window.safeHTML.write('body', contextMenu.outerHTML);
|
||||
document.body.appendChild(contextMenu);
|
||||
|
||||
// position the menu at the cursor position
|
||||
contextMenu.style.display = 'block';
|
||||
contextMenu.style.left = `${e.pageX}px`;
|
||||
contextMenu.style.top = `${e.pageY}px`;
|
||||
|
||||
// event listener to hide the context menu on click
|
||||
document.addEventListener('click', (e) => {
|
||||
if (!contextMenu.contains(e.target)) contextMenu.remove();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
document.removeEventListener('contextmenu', showContextMenu);
|
||||
document.addEventListener('contextmenu', showContextMenu);
|
||||
+2
-1
@@ -89,7 +89,8 @@ ipcRenderer.on('tab-created', (ev, id, url = 'https://duckduckgo.com') => create
|
||||
|
||||
contextBridge.exposeInMainWorld('tabAPI', {
|
||||
ping: () => console.info('pong'),
|
||||
addTab: (url) => ipcRenderer.send('add-tab', url || 'about:blank')
|
||||
addTab: (url) => ipcRenderer.send('add-tab', url || 'about:blank'),
|
||||
newTab: (url) => ipcRenderer.send('add-tab-external', url)
|
||||
});
|
||||
|
||||
const load = async () => {
|
||||
|
||||
Reference in New Issue
Block a user