|
| 1 | +import { UfsGlobal } from "./content-scripts/ufs_global.js"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | icon: "", |
3 | 5 | name: { |
@@ -162,12 +164,95 @@ export default { |
162 | 164 | }, |
163 | 165 |
|
164 | 166 | contentScript: { |
165 | | - onClick_: () => { |
166 | | - console.log(window.location.href); |
167 | | - }, |
| 167 | + // sync element position accross all tabs |
| 168 | + _onDocumentStart: (details) => { |
| 169 | + console.log(details); |
| 170 | + |
| 171 | + const div = document.createElement("div"); |
| 172 | + div.id = "ufs-test"; |
| 173 | + div.innerHTML = ` |
| 174 | + <style> |
| 175 | + #ufs-test { |
| 176 | + position: fixed; |
| 177 | + background-color: #f1f1f1; |
| 178 | + border: 1px solid #d3d3d3; |
| 179 | + text-align: center; |
| 180 | + z-index: 999999999; |
| 181 | + top: 0; |
| 182 | + left: 0; |
| 183 | + } |
| 184 | +
|
| 185 | + #ufs-testheader { |
| 186 | + padding: 10px; |
| 187 | + cursor: move; |
| 188 | + z-index: 10; |
| 189 | + background-color: #2196F3; |
| 190 | + color: #fff; |
| 191 | + } |
| 192 | + </style> |
| 193 | + <div id="ufs-testheader">Click here to move</div> |
| 194 | + <p>Move</p> |
| 195 | + <p>this</p> |
| 196 | + <p>DIV</p>`; |
| 197 | + document.documentElement.appendChild(div); |
| 198 | + |
| 199 | + window.ufs_test = (x, y) => { |
| 200 | + div.style.top = y + "px"; |
| 201 | + div.style.left = x + "px"; |
| 202 | + }; |
| 203 | + |
| 204 | + // Make the DIV element draggable: |
| 205 | + dragElement(div, (x, y) => { |
| 206 | + console.log(x, y); |
| 207 | + chrome.runtime.sendMessage({ action: "ufs-test", data: { x, y } }); |
| 208 | + }); |
| 209 | + |
| 210 | + function dragElement(elmnt, onMoved) { |
| 211 | + var pos1 = 0, |
| 212 | + pos2 = 0, |
| 213 | + pos3 = 0, |
| 214 | + pos4 = 0; |
| 215 | + if (document.getElementById(elmnt.id + "header")) { |
| 216 | + // if present, the header is where you move the DIV from: |
| 217 | + document.getElementById(elmnt.id + "header").onmousedown = |
| 218 | + dragMouseDown; |
| 219 | + } else { |
| 220 | + // otherwise, move the DIV from anywhere inside the DIV: |
| 221 | + elmnt.onmousedown = dragMouseDown; |
| 222 | + } |
| 223 | + |
| 224 | + function dragMouseDown(e) { |
| 225 | + e = e || window.event; |
| 226 | + e.preventDefault(); |
| 227 | + // get the mouse cursor position at startup: |
| 228 | + pos3 = e.clientX; |
| 229 | + pos4 = e.clientY; |
| 230 | + document.onmouseup = closeDragElement; |
| 231 | + // call a function whenever the cursor moves: |
| 232 | + document.onmousemove = elementDrag; |
| 233 | + } |
| 234 | + |
| 235 | + function elementDrag(e) { |
| 236 | + e = e || window.event; |
| 237 | + e.preventDefault(); |
| 238 | + // calculate the new cursor position: |
| 239 | + pos1 = pos3 - e.clientX; |
| 240 | + pos2 = pos4 - e.clientY; |
| 241 | + pos3 = e.clientX; |
| 242 | + pos4 = e.clientY; |
| 243 | + // set the element's new position: |
| 244 | + elmnt.style.top = elmnt.offsetTop - pos2 + "px"; |
| 245 | + elmnt.style.left = elmnt.offsetLeft - pos1 + "px"; |
| 246 | + } |
168 | 247 |
|
169 | | - onDocumentStart_: () => { |
170 | | - console.log("____onDocumentStart"); |
| 248 | + function closeDragElement() { |
| 249 | + // stop moving when mouse button is released: |
| 250 | + document.onmouseup = null; |
| 251 | + document.onmousemove = null; |
| 252 | + |
| 253 | + onMoved?.(parseInt(elmnt.style.left), parseInt(elmnt.style.top)); |
| 254 | + } |
| 255 | + } |
171 | 256 | }, |
172 | 257 |
|
173 | 258 | // text size in KB |
@@ -253,8 +338,50 @@ export default { |
253 | 338 | updateFavicon(); |
254 | 339 | }, |
255 | 340 | }, |
| 341 | + |
| 342 | + backgroundScript: { |
| 343 | + // sync element position accross all tabs |
| 344 | + _onDocumentStart: (details, context) => { |
| 345 | + const cachedPos = context.getCache("ufs-test", { x: 0, y: 0 }); |
| 346 | + updatePos(details.tabId, cachedPos.x, cachedPos.y); |
| 347 | + }, |
| 348 | + _runtime: { |
| 349 | + onMessage: ({ request, sender, sendResponse }, context) => { |
| 350 | + if (request.action === "ufs-test" && request.data) { |
| 351 | + context.setCache("ufs-test", request.data); |
| 352 | + chrome.tabs.query({}, (tabs) => { |
| 353 | + for (let tab of tabs) { |
| 354 | + try { |
| 355 | + updatePos(tab.id, request.data.x, request.data.y); |
| 356 | + } catch (e) {} |
| 357 | + } |
| 358 | + }); |
| 359 | + } |
| 360 | + }, |
| 361 | + }, |
| 362 | + }, |
256 | 363 | }; |
257 | 364 |
|
| 365 | +function updatePos(tabId, x, y) { |
| 366 | + chrome.scripting.executeScript({ |
| 367 | + target: { |
| 368 | + tabId: tabId, |
| 369 | + }, |
| 370 | + func: (x, y) => { |
| 371 | + let interval = setInterval(() => { |
| 372 | + if (typeof window.ufs_test == "function") { |
| 373 | + window.ufs_test?.(x, y); |
| 374 | + clearInterval(interval); |
| 375 | + clearTimeout(timeout); |
| 376 | + } |
| 377 | + }, 100); |
| 378 | + |
| 379 | + let timeout = setTimeout(() => clearInterval(interval), 10000); |
| 380 | + }, |
| 381 | + args: [x, y], |
| 382 | + }); |
| 383 | +} |
| 384 | + |
258 | 385 | const backup = () => { |
259 | 386 | (() => { |
260 | 387 | // modify window.fetch |
|
0 commit comments