Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save derand/48ee3b0f0298199c55786b648d471579 to your computer and use it in GitHub Desktop.
Save derand/48ee3b0f0298199c55786b648d471579 to your computer and use it in GitHub Desktop.
Автоматическое нажатие кнопок на веб странице в Google Chrome Console на JavaScript
//
// Usage: https://youtu.be/VDRx9zJCS9E
//
var dispatchMouseEvent = function(target, var_args) {
var e = document.createEvent("MouseEvents");
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
function mouseClick(target) {
dispatchMouseEvent(target, 'mousedown', true, true);
target.focus();
dispatchMouseEvent(target, 'mouseup', true, true);
};
function elementHasClass(target, cls) {
return (' ' + target.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
var i = 0;
var tmp;
var arr = [];
function timer_func() {
var sug = getElementByXpath('//*[@id="tools"]/div[3]/div[1]/div[2]/div[2]/div/span[1]');
var nextSegmentButton = getElementByXpath('//*[@id="goog.gtc-nextlink"]');
var useSuggestionButton = getElementByXpath('//*[@id="gtc-mtusesugg-button"]/div');
var _ = true;
if (getElementByXpath('//*[@class="gtc-editor-info"]').innerHTML) {
_ = false
}
console.log(i, '/', arr.length, arr[i], ' - ', sug.innerHTML);
if (arr[i]) {
mouseClick(useSuggestionButton);
}
mouseClick(nextSegmentButton);
i += 1;
if (!elementHasClass(nextSegmentButton, 'jfk-button-disabled') && i <= arr.length) {
window.setTimeout(timer_func, 1000);
}
}
var iframe = $x('//iframe')[2]
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
innerDoc.childNodes[1].childNodes[2].childNodes
innerDoc.childNodes[1].childNodes[2].childNodes.forEach(function(el) {
if (el.tagName === 'DIV' && el.getAttribute('class') === 'goog-gtt-messageblock') {
tmp = el.childNodes[2].childNodes[0].childNodes[0].childNodes[0].getAttribute('style');
if (tmp) {
arr.push((' '+tmp.indexOf(' color')) > -1);
} else {
arr.push(true);
}
}
});
console.log('Working array:', arr);
timer_func();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment