// use script below as shortcut in browser to activate... change 'localAddress' below to your actual local network IP address (not localhost,127.0.0.1)
// javascript:(function(){var sq=document.createElement('img');sq.style.cssText='position:fixed;top:0;left:0;width:8px;height:8px;background:#00ff00;z-index:999999;pointer-events:none;';document.body.appendChild(sq);setInterval(()=>{var el=document.querySelector('.ytp-skip-ad-button, [class*="ad-skip-button"], [class*="skip-ad-button"]');if(el&&el.offsetParent!==null){sq.style.background='#ff0000';setTimeout(()=>sq.style.background='#00ff00',1500);var r=el.getBoundingClientRect();var x=Math.round(r.left+4);var y=Math.round(r.top+4);sq.src='http://localAddress:4444/click?x='+x+'&y='+y+'&wx='+window.screenX+'&wy='+window.screenY+'&ww='+window.outerWidth+'&wh='+window.outerHeight+'&t='+Date.now();}},1000);})();

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
    #include <winsock2.h>
    #include <windows.h>
    typedef int socklen_t;
    #pragma comment(lib, "ws2_32.lib")
    #pragma comment(lib, "user32.lib")    
#else
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <X11/Xlib.h>
    #include <X11/extensions/XTest.h>
#endif

#define PORT 4444

void click_mouse(int WinX, int WinY, int x, int y) {
#ifdef _WIN32    
    POINT MyPT = {WinX,WinY};
    HWND hwnd = WindowFromPoint(MyPT);
    LPARAM lClickpos = MAKELPARAM(x,y);
    SendMessage( hwnd , WM_MOUSEMOVE , 0 , lClickpos );
    SendMessage( hwnd , WM_LBUTTONDOWN , 0 , lClickpos );
    SendMessage( hwnd , WM_LBUTTONUP , 0 , lClickpos);    
#else
    Display *dpy = XOpenDisplay(NULL);
    if (dpy) {
        Window root = DefaultRootWindow(dpy);
        Window target = root;
        Window child = None;
        int rx, ry;

        // Traverse the window tree to find the specific window ID under (WinX, WinY)
        while (XTranslateCoordinates(dpy, root, target, WinX, WinY, &rx, &ry, &child) && child != None) {
            target = child;
        }

        Status s_press = 0, s_release = 0;

        if (target != root) {
            // Translate absolute coordinates to local coordinates relative to the found window
            int win_x, win_y;
            XTranslateCoordinates(dpy, root, target, x, y, &win_x, &win_y, &child);

            XEvent event;
            memset(&event, 0, sizeof(event));
            event.xbutton.type = ButtonPress;
            event.xbutton.display = dpy;
            event.xbutton.window = target;
            event.xbutton.root = root;
            event.xbutton.subwindow = None;
            event.xbutton.time = CurrentTime;
            event.xbutton.x = win_x;
            event.xbutton.y = win_y;
            event.xbutton.x_root = x;
            event.xbutton.y_root = y;
            event.xbutton.state = 0;
            event.xbutton.button = Button1;
            event.xbutton.same_screen = True;

            // Send synthetic click directly to the target window
            s_press = XSendEvent(dpy, target, True, ButtonPressMask, &event);
            event.xbutton.type = ButtonRelease;
            event.xbutton.state = Button1Mask;
            s_release = XSendEvent(dpy, target, True, ButtonReleaseMask, &event);
            XFlush(dpy);
        }

        // Fallback to global input event if the window wasn't found or the direct send failed
        if (target == root || s_press == 0 || s_release == 0) {
            XTestFakeMotionEvent(dpy, -1, x, y, CurrentTime);
            XTestFakeButtonEvent(dpy, 1, True, CurrentTime);
            XTestFakeButtonEvent(dpy, 1, False, CurrentTime);
            XFlush(dpy);
        }

        XCloseDisplay(dpy);
    } else {
        printf("Failed to open X display.\n");
    }
#endif
}

int main() {
    int server_fd, client_fd;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

#ifdef _WIN32
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd == -1) {
        printf("Socket creation failed\n");
        return 1;
    }

    int opt = 1;
#ifdef _WIN32
    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
#else
    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#endif

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        printf("Bind failed\n");
        return 1;
    }

    if (listen(server_fd, 3) < 0) {
        printf("Listen failed\n");
        return 1;
    }

    printf("Listening on port %d...\n", PORT);

    while (1) {
        client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);
        if (client_fd < 0) continue;

        memset(buffer, 0, sizeof(buffer));
#ifdef _WIN32
        recv(client_fd, buffer, sizeof(buffer) - 1, 0);
#else
        read(client_fd, buffer, sizeof(buffer) - 1);
#endif

        if (strncmp(buffer, "GET /click", 10) == 0) {
            int x,y , wx,wy , ww,wh;
            char  *x_ptr = strstr(buffer, "x=") ,  *y_ptr = strstr(buffer, "y=");
            char *wx_ptr = strstr(buffer, "wx="), *wy_ptr = strstr(buffer, "wy=");
            char *ww_ptr = strstr(buffer, "ww="), *wh_ptr = strstr(buffer, "wh=");
            if ((x_ptr) && (y_ptr) && (wx_ptr) && (wy_ptr) && (ww_ptr) && (wh_ptr)) {
                x = atoi(x_ptr + 2)+8; y = atoi(y_ptr + 2)+8;
                wx = atoi(wx_ptr + 3); wy = atoi(wy_ptr + 3);
                ww = atoi(ww_ptr + 3); wh = atoi(wh_ptr + 3);
                printf("Triggering click at: X=%d, Y=%d (%d,%d|%dx%d)\n", x, y,wx,wy,ww,wh);
                click_mouse(wx+ww/2,wy+wh/2,x, y);
            } else {
                puts("Invalid request.");
            }
        }
        const char *response = "HTTP/1.1 204 No Content\r\nAccess-Control-Allow-Origin: *\r\n\r\n";
#ifdef _WIN32
        send(client_fd, response, (int)strlen(response), 0);
        closesocket(client_fd);
#else
        write(client_fd, response, strlen(response));
        close(client_fd);
#endif
    }

#ifdef _WIN32
    closesocket(server_fd);
    WSACleanup();
#else
    close(server_fd);
#endif

    return 0;
}