Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit 694ffe7

Browse files
committed
fix: improvements to elements pane
1 parent 21aff84 commit 694ffe7

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/contentScript.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ document.addEventListener(
1818

1919
chrome.runtime.onMessage.addListener((request) => {
2020
if (request.type == "getSuggestedQuery") {
21-
const suggestedQuery = getClosestQuery(currentElement, request.variant);
21+
const { suggestedQuery } = getClosestQuery(currentElement, request.variant);
2222
if (suggestedQuery) {
2323
navigator.clipboard.writeText(suggestedQuery.toString()).then(
2424
() => {},
@@ -36,35 +36,54 @@ chrome.runtime.onMessage.addListener((request) => {
3636
});
3737

3838
function getClosestQuery(element, variant) {
39-
let query = null;
39+
let suggestedQuery = null;
4040
let nextEl = element;
41-
while (!query && nextEl !== document) {
42-
query = window.TestingLibraryDom.getSuggestedQuery(nextEl, variant);
41+
while (!suggestedQuery && nextEl !== document) {
42+
suggestedQuery = window.TestingLibraryDom.getSuggestedQuery(
43+
nextEl,
44+
variant
45+
);
46+
4347
nextEl = nextEl.parentElement;
4448
}
4549

46-
return query;
47-
}
50+
// Why use javascript if you can't use eval from time to time. Deal with it. 😎
51+
// eslint-disable-next-line no-eval
52+
const proposed = window.eval(
53+
`window.TestingLibraryDom.screen.${suggestedQuery
54+
.toString()
55+
.replace("get", "queryAll")}`
56+
);
57+
58+
const exactIndex = proposed.findIndex((el) => el === element);
4859

49-
const variants = ["get", "getAll", "query", "queryAll", "find", "findAll"];
60+
const length = proposed.length;
61+
62+
return { suggestedQuery, length, exactIndex };
63+
}
5064

5165
function showElement(el) {
52-
const suggestion = getClosestQuery(el, "get").toString();
53-
const suggestedQueries = variants.map((variant) =>
54-
suggestion.replace("get", variant)
66+
const { suggestedQuery, length, exactIndex } = getClosestQuery(el, "get");
67+
Bridge.sendMessage(
68+
"show-suggestion",
69+
{
70+
suggestedQuery: suggestedQuery.toString(),
71+
length,
72+
exactIndex,
73+
element: el,
74+
},
75+
"devtools"
5576
);
56-
Bridge.sendMessage("show-el", { suggestedQueries }, "devtools");
5777
}
5878

5979
window.showElement = showElement;
6080

6181
const scriptTag = document.createElement("script");
62-
// TODO: add "script.js" to web_accessible_resources in manifest.json
6382
scriptTag.src = chrome.runtime.getURL(
6483
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js"
6584
);
66-
// eslint-disable-next-line func-names
67-
scriptTag.onload = function () {
85+
86+
scriptTag.onload = function onload() {
6887
this.remove();
6988
};
7089
(document.head || document.documentElement).appendChild(scriptTag);

src/devtools.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@ chrome.devtools.panels.create(
1212
"icons/icon64.png",
1313
"/pages/panel.html"
1414
);
15+
1516
chrome.devtools.panels.elements.createSidebarPane("Which Query?", (sidebar) => {
16-
Bridge.onMessage("show-el", ({ data: { suggestedQueries } }) => {
17-
sidebar.setObject({ suggestedQueries });
18-
});
17+
Bridge.onMessage(
18+
"show-suggestion",
19+
({ data: { suggestedQuery, exactIndex, length } }) => {
20+
const isExact = exactIndex >= 0;
21+
if (length > 1) {
22+
suggestedQuery = suggestedQuery.replace("get", "getAll");
23+
24+
if (isExact) {
25+
suggestedQuery += `[${exactIndex}]`;
26+
}
27+
}
28+
sidebar.setObject(
29+
{ suggestedQuery },
30+
isExact
31+
? "This query will get this precise element"
32+
: "{!} Closest available query, try improving a11y of this element.."
33+
);
34+
}
35+
);
1936
chrome.devtools.panels.elements.onSelectionChanged.addListener(() => {
2037
chrome.devtools.inspectedWindow.eval("window.showElement($0)", {
2138
useContentScriptContext: true,

0 commit comments

Comments
 (0)