101 lines
3.0 KiB
JavaScript
101 lines
3.0 KiB
JavaScript
function createEventJSON(eventType, data) {
|
|
const timeStamp = new Date().toISOString();
|
|
return {
|
|
"@version": 1,
|
|
"type": `browserext.${eventType}`,
|
|
"TimeStamp": timeStamp,
|
|
"data": data,
|
|
"@timestamp": timeStamp
|
|
};
|
|
}
|
|
|
|
document.addEventListener( "keydown", (event) => {
|
|
const key = event.key;
|
|
const code = event.code;
|
|
let description;
|
|
|
|
if (key.match(/^\p{Number}$/u)) {
|
|
description = "number";
|
|
}
|
|
else if (key.match(/^\p{Letter}$/u)) {
|
|
description = "letter";
|
|
}
|
|
else if (key.match(/^\p{Punctuation}$|^\p{Symbol}$/u)) {
|
|
description = "symbol";
|
|
}
|
|
else {
|
|
description = `Клавиша: ${key} Код: ${code}`;
|
|
}
|
|
|
|
const eventData = {description};
|
|
console.log(JSON.stringify(createEventJSON('keydown', eventData)));
|
|
});
|
|
|
|
|
|
document.addEventListener( "mousedown", (event) => {
|
|
const eventData = { button: event.button, description: `Клик мышью: ${event.button}`};
|
|
console.log(JSON.stringify(createEventJSON('mousedown', eventData)));
|
|
});
|
|
|
|
|
|
document.addEventListener( "click", (event) => {
|
|
const clickedElement = event.target;
|
|
const tag = clickedElement.tagName;
|
|
const id = clickedElement.id || "отсутствует";
|
|
const className = clickedElement.className || "отсутствует";
|
|
let eventData;
|
|
|
|
if (tag === 'A') {
|
|
event.preventDefault();
|
|
const href = clickedElement.href;
|
|
eventData = { tag, id, className, href, description: "Клик по ссылке" };
|
|
|
|
localStorage.setItem('clickedElementInfo', JSON.stringify(eventData));
|
|
console.log(JSON.stringify(createEventJSON('click', eventData)));
|
|
|
|
navigateWithPromise(href);
|
|
}
|
|
else if (tag === 'INPUT') {
|
|
const placeholder = clickedElement.placeholder || "отсутствует";
|
|
eventData = { tag, id, className, placeholder, description: "Клик по полю ввода" };
|
|
console.log(JSON.stringify(createEventJSON('click', eventData)));
|
|
}
|
|
});
|
|
|
|
|
|
function navigateWithPromise(href) {
|
|
return new Promise((resolve) => {
|
|
window.location.href = href;
|
|
resolve();
|
|
});
|
|
}
|
|
|
|
|
|
function waitForPageLoad() {
|
|
return new Promise((resolve) => {
|
|
window.addEventListener('load', resolve);
|
|
});
|
|
}
|
|
|
|
|
|
waitForPageLoad().then(() => {
|
|
const clickedElementInfo = localStorage.getItem('clickedElementInfo');
|
|
|
|
if (clickedElementInfo) {
|
|
console.log(clickedElementInfo);
|
|
localStorage.removeItem('clickedElementInfo');
|
|
}
|
|
});
|
|
|
|
|
|
document.addEventListener( "copy", (event) => {
|
|
const pageUrl = window.location.href;
|
|
|
|
const selectedText = window.getSelection().toString();
|
|
let outputText = selectedText.length > 50 ? selectedText.substring(0, 49) + '…' : selectedText;
|
|
|
|
if (selectedText.length > 0) {
|
|
const eventData = { selectedText: outputText, pageUrl, description: "Скопирован текст" };
|
|
console.log(JSON.stringify(createEventJSON('copy', eventData)));
|
|
}
|
|
}); |