responsively-app/desktop-app/app/preload.js

163 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-09-14 07:02:03 +00:00
const {ipcRenderer, remote} = require('electron');
2019-08-27 16:58:05 +00:00
2020-07-04 08:26:23 +00:00
const {Menu} = remote;
const {MenuItem} = remote;
2019-09-14 07:02:03 +00:00
2020-07-04 08:26:23 +00:00
const menu = new Menu();
let rightClickPosition = null;
2019-10-28 13:11:28 +00:00
menu.append(
2020-03-14 08:05:02 +00:00
new MenuItem({
label: 'Take Screenshot',
2020-07-04 08:26:23 +00:00
click(menuItem, browserWindow, event) {
2020-03-14 08:05:02 +00:00
window.responsivelyApp.sendMessageToHost('takeScreenshot');
},
})
2019-10-28 13:11:28 +00:00
);
2019-09-14 07:02:03 +00:00
menu.append(
2020-03-14 08:05:02 +00:00
new MenuItem({
label: 'Tilt Device',
2020-07-04 08:26:23 +00:00
click(menuItem, browserWindow, event) {
2020-03-14 08:05:02 +00:00
window.responsivelyApp.sendMessageToHost('tiltDevice');
},
})
2019-09-14 07:02:03 +00:00
);
2019-10-28 13:11:28 +00:00
menu.append(
2020-03-14 08:05:02 +00:00
new MenuItem({
id: 'mirror-events',
label: 'Mirror Events',
type: 'checkbox',
checked: true,
2020-07-04 08:26:23 +00:00
click(menuItem, browserWindow, event) {
2020-03-14 08:05:02 +00:00
window.responsivelyApp.sendMessageToHost('toggleEventMirroring');
},
})
2019-10-28 13:11:28 +00:00
);
2019-09-14 07:02:03 +00:00
menu.append(new MenuItem({type: 'separator'}));
2019-10-28 13:11:28 +00:00
menu.append(
2020-03-14 08:05:02 +00:00
new MenuItem({
label: 'Inspect',
2020-07-04 08:26:23 +00:00
click(menuItem, browserWindow, event) {
2020-03-14 08:05:02 +00:00
window.responsivelyApp.sendMessageToHost(
'openDevToolsInspector',
rightClickPosition
);
},
})
2019-10-28 13:11:28 +00:00
);
2019-09-14 07:02:03 +00:00
menu.append(
2020-03-14 08:05:02 +00:00
new MenuItem({
label: 'Open Console',
2020-07-04 08:26:23 +00:00
click(menuItem, browserWindow, event) {
2020-03-14 08:05:02 +00:00
window.responsivelyApp.sendMessageToHost('openConsole');
},
})
2019-09-14 07:02:03 +00:00
);
window.addEventListener(
2020-03-14 08:05:02 +00:00
'contextmenu',
2020-07-04 08:26:23 +00:00
e => {
2020-03-14 08:05:02 +00:00
e.preventDefault();
rightClickPosition = {x: e.x, y: e.y};
menu.popup(remote.getCurrentWindow());
},
false
2019-09-14 07:02:03 +00:00
);
global.responsivelyApp = {
2020-03-14 08:05:02 +00:00
sendMessageToHost: (type, message) => {
if (!message) {
message = {};
}
message.sourceDeviceId = window.responsivelyApp.deviceId;
ipcRenderer.sendToHost(type, message);
},
cssPath: el => {
if (!(el instanceof Element)) return;
2020-07-04 08:26:23 +00:00
const path = [];
2020-03-14 08:05:02 +00:00
while (el.nodeType === Node.ELEMENT_NODE) {
2020-07-04 08:26:23 +00:00
let selector = el.nodeName.toLowerCase();
let sib = el;
let nth = 1;
2020-03-14 08:05:02 +00:00
while (
sib.nodeType === Node.ELEMENT_NODE &&
(sib = sib.previousElementSibling) &&
nth++
);
2020-07-04 08:26:23 +00:00
selector += `:nth-child(${nth})`;
2020-03-14 08:05:02 +00:00
path.unshift(selector);
el = el.parentNode;
}
return path.join(' > ');
},
eventFire: (el, etype) => {
if (el.fireEvent) {
2020-07-04 08:26:23 +00:00
el.fireEvent(`on${etype}`);
2020-03-14 08:05:02 +00:00
} else {
2020-07-04 08:26:23 +00:00
const evObj = document.createEvent('Events');
2020-03-14 08:05:02 +00:00
evObj.initEvent(etype, true, false);
evObj.responsivelyAppProcessed = true;
el.dispatchEvent(evObj);
}
},
hideFixedPositionElementsForScreenshot: () => {
const elems = document.body.getElementsByTagName('*');
for (const elem of elems) {
const computedStyle = window.getComputedStyle(elem, null);
2020-07-12 06:56:30 +00:00
if (computedStyle.getPropertyValue('position') === 'fixed') {
2020-03-14 08:05:02 +00:00
elem.classList.add('responsivelyApp__HiddenForScreenshot');
}
}
},
unHideElementsHiddenForScreenshot: () => {
const elems = document.body.querySelectorAll(
'.responsivelyApp__HiddenForScreenshot'
);
for (const elem of elems) {
elem.classList.remove('responsivelyApp__HiddenForScreenshot');
}
},
};
2019-08-16 16:13:38 +00:00
ipcRenderer.on('scrollMessage', (event, args) => {
2020-03-14 08:05:02 +00:00
window.scrollTo({
top: args.y,
left: args.x,
});
});
2019-08-16 16:13:38 +00:00
ipcRenderer.on('clickMessage', (event, args) => {
2020-03-14 08:05:02 +00:00
const elem = document.querySelector(args.cssPath);
if (!elem) {
return;
}
window.responsivelyApp.lastClickElement = elem;
if (elem.click) {
elem.click();
return;
}
window.responsivelyApp.eventFire(elem, 'click');
2019-08-16 16:13:38 +00:00
});
2019-08-17 08:22:53 +00:00
ipcRenderer.on('scrollDownMessage', (event, args) => {
2020-03-14 08:05:02 +00:00
window.scrollTo({
top: window.scrollY + 250,
left: window.scrollX + 250,
behavior: 'smooth',
});
2019-08-17 08:22:53 +00:00
});
ipcRenderer.on('scrollUpMessage', (event, args) => {
2020-03-14 08:05:02 +00:00
window.scrollTo({
top: window.scrollY - 250,
left: window.scrollX - 250,
behavior: 'smooth',
});
2019-08-17 08:22:53 +00:00
});
2019-08-27 16:58:05 +00:00
2019-10-28 13:11:28 +00:00
ipcRenderer.on('eventsMirroringState', (event, args) => {
2020-03-14 08:05:02 +00:00
menu.getMenuItemById('mirror-events').checked = args;
2019-10-28 13:11:28 +00:00
});