-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChrome Object Spy.user.js
128 lines (125 loc) · 5.04 KB
/
Chrome Object Spy.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// ==UserScript==
// @name Chrome Object Spy
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Object Spy for chrome browser
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var arrayWithElements = new Array();
document.onclick = clickListener;
function clickListener(e) {
if(e.target.getAttribute("id")!='dont show popup')
{
var clickedElement;
var clickedElement1;
var arialabel;
var clickedtagname;
var attrname;
var attrnamelen;
var tagdetails = "";
var itext;
if(e === null)
{
var clickedElement = event.srcElement.getAttribute("id");
clickedtagname = event.srcElement.tagName;
}
else
{
var x = e.target.parentNode.nodeName;
clickedElement = e.target.getAttribute("id");
clickedElement1 = e.target.getAttribute("class");
clickedtagname = e.target.tagName;
var xpth = createXPathFromElement(e.target);
arialabel = e.target.getAttribute("aria-label");
itext = e.target.innerText;
if(clickedtagname !== null)
{
tagdetails = tagdetails+"<title>Chrome Object Spy</title><table border='1|0'><tr><th>Property</th><th>Value</th></tr><tr><td>TagName</td><td>"+clickedtagname+"</td></tr>";
}
if(itext !== null)
{
tagdetails = tagdetails+"<tr><td>innertext</td><td>"+ itext+"</td></tr>";
}
if(xpth !== null)
{
tagdetails = tagdetails+"<tr><td>Xpath</td><td>"+ xpth+"</td></tr>";
}
for(var i = 0;i<e.target.attributes.length;i++)
{
attrnamelen = e.target.attributes[i].name.length;
attrname = e.target.attributes[i].name;
var attr1 = e.target.attributes[i].name;
while(attrnamelen<25)
{
attr1 = attr1+ " ";
attrnamelen = attr1.length;
}
attr1 = attr1;
tagdetails = tagdetails +"<tr><td>"+attr1 +"</td><td>"+ e.target.attributes[i].value+"</td></tr>";
}
var a = document.createElement("a");
var dt = 'data:text/html,'+tagdetails;
a.id = 'dont show popup';
a.addEventListener("click", function(){window.open(dt, 'newwindow', 'width=300, height=250'); return false;});
document.getElementsByTagName('body')[0].appendChild(a);
a.click();
}
}
}
function createXPathFromElement(elm) {
var allNodes = document.getElementsByTagName('*');
for (var segs = []; elm && elm.nodeType == 1; elm = elm.parentNode)
{
if (elm.hasAttribute('aria-labelledby')) {
segs.unshift(".//"+elm.tagName.toLowerCase()+"[@aria-labelledby='" + elm.getAttribute('aria-labelledby') + "']<br>");
break;
}
else if(elm.hasAttribute('aria-label')) {
var uniqueIdCount = 0;
for (var n=0;n < allNodes.length;n++) {
if (allNodes[n].hasAttribute('aria-label') && allNodes[n].id == elm.id) uniqueIdCount++;
if (uniqueIdCount > 1) break;
}
if ( uniqueIdCount == 1) {
segs.unshift(".//"+elm.tagName.toLowerCase()+"[@aria-label='" + elm.getAttribute('aria-label') + "']<br>");
return segs;
} else {
segs.unshift(".//"+elm.tagName.toLowerCase()+"[@aria-label='" + elm.getAttribute('aria-label') + "']<br>");
}
break;
//segs.unshift(elm.localName.toLowerCase() + ".//"+elm.tagName+"[@aria-label='" + elm.getAttribute('aria-label') + "']");
}
else if (elm.hasAttribute('id')) {
var uniqueIdCount = 0;
for (var n=0;n < allNodes.length;n++) {
if (allNodes[n].hasAttribute('id') && allNodes[n].id == elm.id) uniqueIdCount++;
if (uniqueIdCount > 1) break;
}
if ( uniqueIdCount == 1) {
segs.unshift(".//"+elm.tagName.toLowerCase()+"[@id='" + elm.getAttribute('id') + "']<br>");
return segs;
} else {
segs.unshift(".//"+elm.tagName.toLowerCase()+"[@id='" + elm.getAttribute('id') + "']<br>");
}
break;
} else if (elm.hasAttribute('class')) {
segs.unshift(".//"+elm.tagName.toLowerCase()+"[@class='" + elm.getAttribute('class') + "']<br>");
break;
} else {
for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) {
if (sib.localName == elm.localName) i++; }
segs.unshift(elm.localName.toLowerCase() + '[' + i + ']');
}
}
return segs.length ? '' + segs : null;
}
function lookupElementByXPath(path) {
var evaluator = new XPathEvaluator();
var result = evaluator.evaluate(path, document.documentElement, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return result.singleNodeValue;
}
})();