IPC communication with usng preload


#1

I am tring to communicate with my webview.
This is printing alert “sending” when right click on the webview but message is not received in render process.
How do i receive messages.

preload.js

//Inject this file into competitor website while loading to communicate with competitor webage

var ipcRenderer = require('electron').ipcRenderer; //used to communicate with webview


document.addEventListener("click", function (e) {

    var data = {
        href: false
    };

    if(data.href) {
        ipcRenderer.sendToHost("clicked-href", data);
    }
    return true;

});

// Get Document title once it has been loaded
document.addEventListener("DOMContentLoaded", function () {

    var data = {
        "title": document.title,
        "url": window.location.href
    };

    ipcRenderer.sendToHost("window-data", data);
    //ipcRenderer.sendToHost('html-content' , document.body.innerHTML);

});

document.addEventListener('dragover', event => event.preventDefault());
document.addEventListener('drop', event => event.preventDefault());


screen = new function() { this.width = 1200; this.height = 800 };



// Context menu data for webview
document.addEventListener('contextmenu', function(e) {

    var data = {
        x: e.pageX,
        y: e.pageY,
        hasSelection: !!window.getSelection.toString(),
        href: false,
        img: false,
        video: false
    };

    var el = document.elementFromPoint(e.pageX - window.pageXOffset, e.pageY - window.pageYOffset);

    while (el && el.tagName) {
        if (!data.img && el.tagName == 'IMG')
            data.img = el.src;
        if (!data.href && el.href)
            data.href = el.href;
        el = el.parentNode;
    }

    alert("sending")
    ipcRenderer.sendToHost('contextmenu-data', data);

});

Object.defineProperty(navigator, 'plugins', { value: [] });
Object.defineProperty(navigator, 'mimeTypes', { value: '' });
Object.defineProperty(navigator, 'mediaDevices', { value: '' });

render.js

    var webview = document.getElementsByClassName("tabs-pane active")[0];
    webview.addEventListener("ipc-message", function (e) {

        alert("receiver")


        if (e.channel === "window-data") {
            //var current_title = e.args[0].title.substring(0,10);
            trigger_tab_load_icon(webview,req_given_by);
            $("#-url-bar").val(e.args[0].url);
        }

        if (e.channel === "html-content") {
            //console.log(e.args[0])
        }

        if (e.channel === "clicked-href") {
            // console.log(e.args[0].href);
            if (e.args[0].href) {
                open_newtab_hit_url(e.args[0].href);
            }
        }
        if (e.channel === "contextmenu-data") {
            // console.log(e.args[0]);
            webviewContextMenu(e.args[0]);
        }

        $("webview.active").focus();
    });

I tried

ipcRenderer.on(channel, listener)

instead of webview.addEventListener("ipc-message", function (e) { but no effect

Thanks
Jk