@nightwatch/nightwatch-inspector
Version:
Nightwatch Inspector that allows you to tests command directly from the browser
72 lines (55 loc) • 2.06 kB
JavaScript
/* eslint-disable no-undef */
/**
* ADD ROW TO SELECTOR HISTORY IN EXPLORE MODE
*/
function getSelectorFromFirstCell(event) {
const targetElement = event.target.parentElement.parentElement.firstElementChild;
return targetElement.textContent;
}
function copyToClipboard(value) {
const textarea = document.createElement('textarea');
textarea.textContent = value;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
function clickOnCopy(event) {
// Info: clipboard API not working. Using deprecated execCommand function
const selectorValue = getSelectorFromFirstCell(event);
copyToClipboard(selectorValue);
}
function clickOnHighlight(event) {
const selectorValue = getSelectorFromFirstCell(event);
sendMessageToBackground('HIGHLIGHT_ELEMENT', selectorValue);
}
function addRow(selector) {
const selectorTable = document.getElementById('selectorTable');
const tbody = selectorTable.getElementsByTagName('tbody')[0];
const newRow = tbody.insertRow(0);
const highlightButton = document.createElement('button');
highlightButton.classList.add('button-default');
highlightButton.appendChild(document.createTextNode('Highlight'));
highlightButton.addEventListener('click', clickOnHighlight);
const copyButton = document.createElement('button');
copyButton.classList.add('button-default');
copyButton.appendChild(document.createTextNode('Copy'));
copyButton.addEventListener('click', clickOnCopy);
var newCell = newRow.insertCell();
newCell.style.width = '65%';
newCell.appendChild(document.createTextNode(selector));
newCell = newRow.insertCell();
newCell.style.width = '35%';
newCell.appendChild(highlightButton);
newCell.appendChild(copyButton);
}
chrome.runtime.onMessage.addListener((msg, sender, response) => {
const {from, action, content} = msg;
if (sender.tab.id === tabID && from === 'contentJS') {
switch (action) {
case 'selector':
addRow(content);
break;
}
}
});