EasySpider/ElectronJS/bootstrap/baozhuangscript.js
2023-02-07 16:30:43 +08:00

137 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function bzreadXPath(element) {
if (element.id !== "") {//判断id属性如果这个元素有id则显 示//*[@id="xPath"] 形式内容
return '//*[@id=\"' + element.id + '\"]';
}
//这里需要需要主要字符串转译问题可参考js 动态生成html时字符串和变量转译注意引号的作用
if (element == document.body) {//递归到body处结束递归
return '/html/' + element.tagName.toLowerCase();
}
var ix = 1,//在nodelist中的位置且每次点击初始化
siblings = element.parentNode.childNodes;//同级的子元素
for (var i = 0, l = siblings.length; i < l; i++) {
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素则执行递归操作
if (sibling == element) {
return arguments.callee(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix) + ']';
//如果不符合判断是否是element元素并且是否是相同元素如果是相同的就开始累加
} else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
ix++;
}
}
};
console.log("sdf");
//创造div作为选中元素后的样式存在
var bzdiv = document.createElement('div');
bzdiv.style.zIndex=-2147483647;
bzdiv.style.position = "fixed";
bzdiv.style.boxSizing = "border-box";
bzdiv.style.border = "dotted";
//右键菜单屏蔽
document.oncontextmenu = () => false;
var nodeList = [];
NowNode = null;
var xnode = null;
var style = ""; //记录上个元素的颜色
document.addEventListener("mousemove", function () {
oe = document.elementFromPoint(event.x, event.y);
NowNode = oe;
te = 0;
exist = 0;
for (o of nodeList) {
if (o["node"] == oe) {
exist = 1;
break;
}
}
// console.log(oe);
if (xnode == null) {
xnode = oe;
}
if (xnode != oe) {
xnode.style.backgroundColor = style; //上个元素改回原来元素的背景颜色
style = oe.style.backgroundColor;
// oe.addEventListener("mousedown",addEl());
// xnode.removeEventListener("mousedown",addEl());
console.log(oe);
if (exist == 1) {
console.log("exist");
}
else {
oe.style.backgroundColor = 'rgba(221,221,255,0.8)'; //设置新元素的背景元素
}
xnode = oe;
bzdiv.style.display = "none";
}
});
//点击没反应时候的替代方案
document.onkeydown = function (event) {
console.log("keydown");
var e = event || window.event || arguments.callee.caller.arguments[0];
if (e && e.keyCode == 118) { // 按 F7
bzaddEl();
}
else if (e && e.keyCode == 119) {//按F8
bzclear();
}
else if (e && e.keyCode == 120) {//按F9
NowNode.click();
console.log("click",NowNode);
}
};
//选中元素到列表中
function bzaddEl() {
exist = 0;
for (o of nodeList) {
if (o["node"] == oe) {
exist = 1;
break;
}
}
//元素没有被添加过才去添加
if (exist == 0) {
nodeList.push({ node: NowNode, bgcolor: style, xpath: bzreadXPath(NowNode) });
NowNode.style.backgroundColor = "rgba(0,191,255,0.5)";
style = "rgba(0,191,255,0.5)";
//将虚线框显示在元素上方但屏蔽其鼠标操作
var pos = NowNode.getBoundingClientRect();
bzdiv.style.display="block";
bzdiv.style.height = NowNode.offsetHeight + "px";
bzdiv.style.width = NowNode.offsetWidth+ "px";
bzdiv.style.left = pos.left+ "px";
bzdiv.style.top = pos.top+ "px";
bzdiv.style.zIndex=2147483647;
bzdiv.style.pointerEvents = "none";
}
console.log(nodeList);
//对于可点击元素屏蔽a标签默认点击事件
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault ? event.preventDefault() : event.returnValue = false;
}
document.addEventListener("mousedown", bzaddEl);
//清除选择项
function bzclear() {
//如果最后停留的元素被选中则调整此元素的style为原始style否则不进行调整
for (node of nodeList) {
node["node"].style.backgroundColor = node["bgcolor"];
if (NowNode == node["node"]) {
style = node["bgcolor"];
}
}
nodeList.splice(0, nodeList.length); //清空数组
}
bztimer = setInterval(function() {
if(document.body != null)
{
clearInterval(bztimer);
document.body.append(bzdiv);
}
}, 100);