-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
161 lines (138 loc) · 4.99 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const getDarkModeValue = async () => {
const result = await chrome.storage.sync.get("darkModeActive");
return result.darkModeActive;
};
const showPopup = (darkMode, prompt, textToInject) => {
try {
//check if the popup is already open
let popup = document.querySelector(".popup-bing-ai-unique-class-name");
if (popup) {
//if injectedText is not empty
if (textToInject) {
popup.classList.remove("hidden");
popup.style.transform = "scale(1)";
//get the iframe element
const iframe = popup.querySelector("iframe");
// send a message to the iframe to inject the text
// this message will be received in the script loaded in the iframe
iframe.contentWindow.postMessage(
{ prompt: prompt, textToInject: textToInject },
"*"
);
return;
}
//if the popup is hidden, show it and return
if (popup.classList.contains("hidden")) {
popup.classList.remove("hidden");
popup.style.transform = "scale(1)";
return;
}
// if the popup is visible, hide it and return
popup.style.transform = "scale(0)";
popup.classList.add("hidden");
return;
}
// if the popup doesn't exist, create it
// create a div element to hold the popup content
popup = document.createElement("div");
popup.style.cssText = `
position: fixed;
z-index: 99999;
top: 0;
right: 0;
width: 500px;
height: 600px;
display: block;
background: ${darkMode ? "rgb(43, 43, 43)" : "white"};
margin: 15px;
border-radius: 10px;
border: 1px solid black;
overflow: hidden;
transform-origin: top right;
transform: scale(0);
box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;
transition: transform 0.2s ease-in-out;
padding: 0;
`;
popup.className = "popup-bing-ai-unique-class-name";
// create an iframe element to hold the bing ai page
const iframe = document.createElement("iframe");
const darkModeValue = darkMode ? "darkschemeovr" : "lightschemeovr";
iframe.src = `https://edgeservices.bing.com/edgediscover/query?&FORM=SHORUN&udscs=1&udsnav=1&setlang=en-US&${darkModeValue}=1&features=udssydinternal&clientscopes=windowheader,coauthor,chat,&udsframed=1`;
iframe.style.cssText = `
background: ${darkMode ? "rgb(43, 43, 43)" : "white"};
width: 100%;
height: 100%;
border: none;
border-radius: 10px;
margin: 0;
padding: 0;
`;
iframe.className = "popup-iframe-bing-ai-unique-class-name";
//allow the iframe to copy to clipboard
iframe.setAttribute("allow", "clipboard-read *; clipboard-write");
// append the iframe to the popup
popup.appendChild(iframe);
// append the popup element to the document body
document.body.parentNode.appendChild(popup);
const iframeLoadEventHandler = async () => {
iframe.removeEventListener('load', iframeLoadEventHandler);
iframe.contentWindow.postMessage(
{ prompt, textToInject },
"*"
);
};
// this will trigger the CSS transition and animate the transform property
popup.style.transform = "scale(1)";
// this check is needed because the if the popup is just created, it could also be called with a textToInject parameter
if (prompt && textToInject) {
//wait for the iframe to load
iframe.addEventListener("load", iframeLoadEventHandler);
}
} catch (e) {
console.error(e);
}
};
// Add a listener to create the initial context menu items
const onInstalledHandler = async () => {
chrome.contextMenus.create({
id: 'bingai',
title: 'Bing AI',
contexts: ['selection'],
});
['Summarize', 'Explain', 'Answer', 'Translate'].forEach((title) => {
chrome.contextMenus.create({
title,
contexts: ['selection'],
parentId: 'bingai',
id: title.toLowerCase(),
});
});
};
chrome.runtime.onInstalled.addListener(onInstalledHandler);
const contextMenuClickHandler = async ({ selectionText, menuItemId: prompt }, { id: tabId }) => {
const darkModeActive = await getDarkModeValue();
chrome.scripting.executeScript(
{
target: { tabId },
args: [darkModeActive ? true : false, prompt, selectionText],
function: showPopup,
},
() => chrome.runtime.lastError
);
};
chrome.contextMenus.onClicked.addListener(contextMenuClickHandler);
// listen for the extension to be clicked and run the function showPopup
const actionClickHandler = async ({ id: tabId }) => {
const darkModeActive = await getDarkModeValue();
// pass the value of "darkModeActive" as an argument to "showPopup"
chrome.scripting.executeScript(
{
target: { tabId },
args: [darkModeActive ? true : false],
function: showPopup,
},
() => chrome.runtime.lastError
);
};
chrome.action.onClicked.addListener(actionClickHandler);