Skip to content

Commit

Permalink
Merge pull request #7 from AlexInABox/dev
Browse files Browse the repository at this point in the history
cleanup + added delay before logic begin
  • Loading branch information
AlexInABox authored Nov 15, 2024
2 parents 4dcc6be + 9a34418 commit d04e910
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 63 deletions.
138 changes: 81 additions & 57 deletions contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,70 @@ function isLocalNetworkURL(url) {
return localNetworkRegex.test(url);
}

(async () => {
let URL = window.location.toString();
//console.log("cursors: contentScript.js: URL: " + URL);

if (isLocalNetworkURL(URL)) {
//console.log("cursors: contentScript.js: URL is on the local-network. Exiting...");
return;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

//check if the url is in the blacklist
async function isURLBlacklisted(URL) {
let blacklist = await browser.storage.local.get(["cursors.blacklist"]);
if (!blacklist["cursors.blacklist"]) {
console.log("cursors: contentScript.js: blacklist not found. Creating blacklist...");
await browser.storage.local.set({ "cursors.blacklist": [] });
blacklist = await browser.storage.local.get(["cursors.blacklist"]);
}

//console.log("cursors: contentScript.js: blacklist: " + blacklist["cursors.blacklist"]);
if (blacklist["cursors.blacklist"].includes(URL)) {
//console.log("contentScript.js: " + URL + " is blacklisted.");
return;
}
console.log(blacklist["cursors.blacklist"].includes(URL))
return (blacklist["cursors.blacklist"].includes(URL));
}

var skinId = 0;
async function getSkinIdFromStorage() {
let skinId;
browser.storage.local.get("cursors.customization.skinId", function (result) {
if (result["cursors.customization.skinId"]) {
console.log("cursors: contentScript.js: user has customization");
skinId = result["cursors.customization.skinId"];
if (skinId) {
console.log("cursors: contentScript.js: skinId: " + skinId);
}
}
});

if (skinId == undefined) {
return 0;
}
return skinId;
}

window.addEventListener("load", () => {
(async () => {
try {
await sleep(2000); // This might circumvent some random URL changes or security policies for now
main();
} catch (error) {
console.error("Error in isolated async logic:", error);
}
})();
});

async function main() {
console.log("Good morning america!!");
let URL = window.location.toString();
if (isLocalNetworkURL(URL)) {
console.log("isLocal");
return;
}

if (await isURLBlacklisted()) {
console.log("isBlacklisted");
return;
}

var skinId = await getSkinIdFromStorage();
console.log(skinId)

let mousePosition = {
x: -2000,
y: -2000
y: -2000,
lastX: -2000,
lastY: -2000
};
let cursorUserCounter = 0;


browser.runtime.onMessage.addListener((obj, sender, response) => {
if (obj.type == "fetch-user-count") {
console.log("cursors: fetch-user-count: " + cursorUserCounter);
response(cursorUserCounter);
}
});

window.addEventListener('mousemove', (event) => {
//get the cursor position
Expand Down Expand Up @@ -129,17 +146,15 @@ function isLocalNetworkURL(url) {
}

//send cursor position every 10ms
var lastX = -2000;
var lastY = -2000;
setInterval(function () {
if (lastX == mousePosition.x && lastY == mousePosition.y || ws.readyState != 1) { //if the cursor position hasn't changed or the websocket isn't open
if (mousePosition.lastX == mousePosition.x && mousePosition.lastY == mousePosition.y || ws.readyState != 1) { //if the cursor position hasn't changed or the websocket isn't open
return;
}
lastX = mousePosition.x;
lastY = mousePosition.y;
mousePosition.lastX = mousePosition.x;
mousePosition.lastY = mousePosition.y;
//send the cursor position to the server
try {
ws.send(JSON.stringify({ type: "cursor-update", x: lastX, y: lastY }));
ws.send(JSON.stringify({ type: "cursor-update", x: mousePosition.lastX, y: mousePosition.lastY }));
} catch (error) {
console.log("cursors: We ran into an WebSocket related error when sendin a message. No need to alarm google tho... Here: " + String(error));
}
Expand All @@ -148,15 +163,15 @@ function isLocalNetworkURL(url) {

ws.onclose = function (event) {

cursorUserCounter = 0;
connectedUserCounter = 0;
clearInterval(keepaliveInterval);

console.log("cursors: websocket closed for reason: " + event.code);
}

ws.onerror = function (error) {

cursorUserCounter = 0;
connectedUserCounter = 0;
clearInterval(keepaliveInterval);

console.log("cursors: We ran into an WebSocket related error. No need to alarm google tho...");
Expand All @@ -171,7 +186,7 @@ function isLocalNetworkURL(url) {
const addClient = (id, skinId) => {
//console.log("contentScript.js: addClient: adding client with id: " + id + " and skinId: " + skinId);

cursorUserCounter++;
connectedUserCounter++;

//create a new cursor element
var cursor = document.createElement("img");
Expand All @@ -192,14 +207,15 @@ function isLocalNetworkURL(url) {

const removeClient = (id) => {
//remove the cursor from the page
cursorUserCounter--;
connectedUserCounter--;

document.body.removeChild(document.getElementById(id));
}

const updateCursor = (id, x, y) => {
//get the cursor
var cursor = document.getElementById(id);
console.log(cursor);
//update the cursor position
//cursor.style.transition = 'transform 0.1s ease'; // Adjust the transition duration as needed
//cursor.style.transform = `translate(${x}px, ${y}px)`;
Expand Down Expand Up @@ -250,27 +266,27 @@ function isLocalNetworkURL(url) {
style.id = "CursorConnectUniqueCSSStyle";
//set the style element content
style.innerHTML = `
.CursorConnectUniqueCSSClass {
position: absolute;
transform: translate(-33%, -23%);
left: -2000px;
top: -2000px;
pointer-events: none;
width: 40px;
z-index: 999999;
opacity: 1;
animation: fadeOut 10s forwards cubic-bezier(1,0,.8,.35);
}
@keyframes fadeOut {
0% {
.CursorConnectUniqueCSSClass {
position: absolute;
transform: translate(-33%, -23%);
left: -2000px;
top: -2000px;
pointer-events: none;
width: 40px;
z-index: 999999;
opacity: 1;
animation: fadeOut 10s forwards cubic-bezier(1,0,.8,.35);
}
100% {
opacity: 0;
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
}
`;
`;
//add the style element to the page

document.head.appendChild(style);
Expand Down Expand Up @@ -299,4 +315,12 @@ function isLocalNetworkURL(url) {
connectToWebSocket();
}
}, 1000);
})();
}

var connectedUserCounter = 0;
browser.runtime.onMessage.addListener((obj, sender, response) => {
if (obj.type == "fetch-user-count") {
console.log("cursors: fetch-user-count: " + connectedUserCounter);
response(connectedUserCounter);
}
});
4 changes: 2 additions & 2 deletions platform/chromium/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CursorConnect",
"version": "0.1.6",
"version": "0.1.7",
"description": "Real-time cursor interaction for a more connected browsing experience.",
"permissions": [
"storage",
Expand All @@ -15,7 +15,7 @@
"js": [
"contentScript.js"
],
"run_at": "document_end"
"run_at": "document_start"
}
],
"web_accessible_resources": [
Expand Down
19 changes: 15 additions & 4 deletions platform/firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CursorConnect",
"version": "0.1.6",
"version": "0.1.7",
"description": "Real-time cursor interaction for a more connected browsing experience.",
"browser_specific_settings": {
"gecko": {
Expand All @@ -21,10 +21,21 @@
"js": [
"contentScript.js"
],
"run_at": "document_end"
"run_at": "document_start"
}
],
"browser_action": {
"web_accessible_resources": [
{
"resources": [
"assets/cursor.png",
"customization/*.png"
],
"matches": [
"*://*/*"
]
}
],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "assets/icon16.png",
Expand All @@ -33,5 +44,5 @@
"128": "assets/icon128.png"
}
},
"manifest_version": 2
"manifest_version": 3
}

0 comments on commit d04e910

Please sign in to comment.