2023-05-18 03:32:32 -04:00
|
|
|
/**
|
|
|
|
* @name popup.core.js
|
|
|
|
* @version 0.1.0
|
|
|
|
* @url https://github.com/lencx/ChatGPT/tree/main/scripts/popup.core.js
|
|
|
|
*/
|
2023-01-07 23:28:54 -05:00
|
|
|
|
2023-05-24 21:52:33 -04:00
|
|
|
async function popupCoreInit() {
|
2023-05-18 03:32:32 -04:00
|
|
|
const chatConf = (await invoke('get_app_conf')) || {};
|
2023-01-07 23:28:54 -05:00
|
|
|
if (!chatConf.popup_search) return;
|
|
|
|
if (!window.FloatingUIDOM) return;
|
|
|
|
|
|
|
|
const styleDom = document.createElement('style');
|
|
|
|
styleDom.innerHTML = `
|
|
|
|
#chagpt-selection-menu {
|
|
|
|
display: none;
|
|
|
|
width: max-content;
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
background: #4a4a4a;
|
|
|
|
color: white;
|
|
|
|
font-weight: bold;
|
2023-01-15 03:14:37 -05:00
|
|
|
padding: 3px 5px;
|
|
|
|
border-radius: 2px;
|
|
|
|
font-size: 10px;
|
2023-01-07 23:28:54 -05:00
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
document.head.append(styleDom);
|
|
|
|
|
|
|
|
const selectionMenu = document.createElement('div');
|
|
|
|
selectionMenu.id = 'chagpt-selection-menu';
|
|
|
|
selectionMenu.innerHTML = 'DALL·E 2';
|
|
|
|
document.body.appendChild(selectionMenu);
|
|
|
|
const { computePosition, flip, offset, shift } = window.FloatingUIDOM;
|
|
|
|
|
|
|
|
document.body.addEventListener('mousedown', async (e) => {
|
2023-01-15 03:14:37 -05:00
|
|
|
selectionMenu.style.display = 'none';
|
2023-01-07 23:28:54 -05:00
|
|
|
if (e.target.id === 'chagpt-selection-menu') {
|
2023-05-18 03:32:32 -04:00
|
|
|
await invoke('dalle2_search_window', {
|
|
|
|
query: encodeURIComponent(window.__DALLE2_CONTENT__),
|
|
|
|
});
|
2023-01-07 23:28:54 -05:00
|
|
|
} else {
|
|
|
|
delete window.__DALLE2_CONTENT__;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-05-18 03:32:32 -04:00
|
|
|
document.body.addEventListener('mouseup', async (e) => {
|
2023-01-07 23:28:54 -05:00
|
|
|
selectionMenu.style.display = 'none';
|
|
|
|
const selection = window.getSelection();
|
|
|
|
window.__DALLE2_CONTENT__ = selection.toString().trim();
|
|
|
|
|
|
|
|
if (!window.__DALLE2_CONTENT__) return;
|
|
|
|
|
|
|
|
if (selection.rangeCount > 0) {
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
const rect = range.getClientRects()[0];
|
|
|
|
|
|
|
|
const rootEl = document.createElement('div');
|
|
|
|
rootEl.style.top = `${rect.top}px`;
|
|
|
|
rootEl.style.position = 'fixed';
|
|
|
|
rootEl.style.left = `${rect.left}px`;
|
|
|
|
document.body.appendChild(rootEl);
|
|
|
|
|
|
|
|
selectionMenu.style.display = 'block';
|
|
|
|
computePosition(rootEl, selectionMenu, {
|
|
|
|
placement: 'top',
|
2023-05-18 03:32:32 -04:00
|
|
|
middleware: [flip(), offset(5), shift({ padding: 5 })],
|
|
|
|
}).then(({ x, y }) => {
|
2023-01-07 23:28:54 -05:00
|
|
|
Object.assign(selectionMenu.style, {
|
|
|
|
left: `${x}px`,
|
|
|
|
top: `${y}px`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-01-15 05:02:23 -05:00
|
|
|
}
|
|
|
|
|
2023-05-18 03:32:32 -04:00
|
|
|
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
2023-05-24 21:52:33 -04:00
|
|
|
popupCoreInit();
|
2023-01-15 05:02:23 -05:00
|
|
|
} else {
|
2023-05-24 21:52:33 -04:00
|
|
|
document.addEventListener('DOMContentLoaded', popupCoreInit);
|
2023-01-15 05:02:23 -05:00
|
|
|
}
|