Skip to content

Instantly share code, notes, and snippets.

@simonkuang
Created May 7, 2020 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonkuang/a007c1e3d9974b6a41265efca31e9a85 to your computer and use it in GitHub Desktop.
Save simonkuang/a007c1e3d9974b6a41265efca31e9a85 to your computer and use it in GitHub Desktop.
查询 twitter 登录页面中,用户名、密码框、登录按钮在页面中位置的脚本
window["setPosition"] = (...args) => { console.log(args); };
window["jsperform"] = (...args) => { console.log(args); };
// 探测登录页面中,用户名文本框、密码文本框和登录按钮的位置
;(function() {
// 退出前做的工作
var exit = () => {
// 通知机器人 js 执行完毕
window.jsperform("login");
}, setPosition = (x, y) => {
window.jsperform("setPosition", x, y);
};
var username = document.querySelector('[name="session[username_or_email]"]');
var password = document.querySelector('[name="session[password]"]');
if (!username || !password) {
console.error("Cannot find the input element 'username' or 'password'.");
exit();
return ;
}
var button = document.querySelector('[data-testid="LoginForm_Login_Button"]');
if (!button) {
console.error("Cannot find the login button.");
exit();
return ;
}
// 获取元素的绝对位置坐标(像对于页面左上角)
function getElementPagePosition(element) {
//计算 x, y 坐标
var actualLeft = element.offsetLeft;
var actualTop = element.offsetTop;
var current = element.offsetParent;
while (current !== null) {
actualLeft += current.offsetLeft;
actualTop += (current.offsetTop + current.clientTop);
current = current.offsetParent;
}
//返回结果
return {x: actualLeft, y: actualTop};
};
// 获取元素的绝对位置坐标(像对于浏览器视区左上角)
function getElementViewPosition(element) {
var actualLeft = element.offsetLeft;
var actualTop = element.offsetTop;
var current = element.offsetParent;
while (current !== null) {
actualLeft += (current.offsetLeft + current.clientLeft);
actualTop += (current.offsetTop + current.clientTop);
current = current.offsetParent;
}
if (document.compatMode == "BackCompat"){
var elementScrollLeft = document.body.scrollLeft;
var elementScrollTop = document.body.scrollTop;
} else {
var elementScrollLeft = document.documentElement.scrollLeft;
var elementScrollTop = document.documentElement.scrollTop;
}
var left = actualLeft - elementScrollLeft;
var right = actualTop - elementScrollTop;
return {x: left, y: right};
};
// 获取元素外边界的大小
function getElementSize(element) {
return {"width": element.offsetWidth, "height": element.offsetHeight};
}
// 获取元素大小
function getElementContentSize(element) {
var direction = ['width', 'height'],
ret = {"width": element.offsetWidth, "height": element.offsetHeight},
which, value, style, getStyle = function(el) {
if ('getComputedStyle' in window) {
return window.getComputedStyle(el, null);
} else if ('currentStyle' in el) {
return el.currentStyle;
} else {
return el.style;
}
};
for (var i in direction) {
which = direction[i] === 'width' ? ['Left', 'Right'] : ['Top', 'Bottom'];
value = ret[direction[i]];
style = getStyle(element);
for (var j in which) {
var a = which[j];
value -= parseFloat(style[`border${a}Width`]);
value -= parseFloat(style[`padding${a}`]);
}
}
return ret;
};
var username_position = getElementViewPosition(username),
username_size = getElementSize(username),
password_position = getElementViewPosition(password),
password_size = getElementSize(password),
button_position = getElementViewPosition(button),
button_size = getElementSize(button);
// 设置
if ('setPosition' in window) {
var usr_x, usr_y, pwd_x, pwd_y, btn_x, btn_y,
randomPosition = (m, n) => {
return parseInt(m + n - Math.random() * (n / 2));
};
usr_x = randomPosition(username_position["x"], username_size["width"]);
usr_y = randomPosition(username_position["y"], username_size["height"]);
pwd_x = randomPosition(password_position["x"], password_size["width"]);
pwd_y = randomPosition(password_position["y"], password_size["height"]);
btn_x = randomPosition(button_position["x"], button_size["width"]);
btn_y = randomPosition(button_position["y"], button_size["height"]);
// 通知机器人
setPosition('username', usr_x, usr_y);
setPosition('password', pwd_x, pwd_y);
setPosition('button', btn_x, btn_y);
}
exit();
return ;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment