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

Commit 322ad67

Browse files
committed
fix: fixed copy bug on non ssl, non focused pages
1 parent b7d9e5c commit 322ad67

File tree

1 file changed

+13
-47
lines changed

1 file changed

+13
-47
lines changed

src/contentScript.js

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,23 @@ Bridge.onMessage("run-query-in-console", ({ data: { query } }) => {
2222
/* eslint-enable */
2323
});
2424

25-
const CANT_COPY_NOTIFICATION = {
26-
type: "basic",
27-
iconUrl: "icons/icon64.png",
28-
title: "Can't Copy Query",
29-
message: "Check browser console for details.",
30-
contextMessage: "Are you are debugging?",
31-
};
32-
3325
chrome.runtime.onMessage.addListener((request) => {
3426
if (request.type == "getSuggestedQuery") {
3527
const { suggestedQuery } = getClosestQuery(currentElement, request.variant);
3628
if (suggestedQuery) {
3729
const queryToCopy = `screen.${suggestedQuery.toString()}`;
38-
navigator.clipboard.writeText(queryToCopy).then(
39-
() => {
40-
// TODO: add option to toggle this
41-
Bridge.sendMessage(
42-
"show-notification",
43-
{
44-
notification: {
45-
type: "basic",
46-
iconUrl: "icons/icon64.png",
47-
title: "Copied Query",
48-
message: queryToCopy,
49-
},
50-
},
51-
"background"
52-
);
53-
},
54-
(err) => {
55-
/* eslint-disable no-console */
56-
Bridge.sendMessage(
57-
"show-notification",
58-
{ notification: CANT_COPY_NOTIFICATION },
59-
"background"
60-
);
61-
62-
// TODO: figure this crap out https://github.com/testing-library/which-query/issues/9
63-
console.warn(
64-
`
65-
Can't copy query to clipboard when focus is not in the browser.
66-
Click in the page and be sure devtools does not have focus when using Testing Library copy menus.
67-
Know how to fix this issue? Pull Requests welcome: https://github.com/testing-library/which-query/issues/9
68-
`,
69-
err
70-
);
71-
console.log("Here is the query you tried to copy:");
72-
console.log(queryToCopy);
73-
/* eslint-enable no-console */
74-
}
75-
);
30+
const currentEl = document.activeElement;
31+
32+
const hiddenInput = document.createElement("input");
33+
hiddenInput.type = "text";
34+
hiddenInput.value = queryToCopy;
35+
document.body.appendChild(hiddenInput);
36+
hiddenInput.select();
37+
hiddenInput.focus();
38+
39+
document.execCommand("copy");
40+
hiddenInput.remove();
41+
currentEl.focus();
7642
}
7743
}
7844
});
@@ -131,7 +97,7 @@ function injectScript(scriptPath) {
13197

13298
scriptTag.onload = function onload() {
13399
resolve();
134-
this.remove();
100+
// this.remove();
135101
};
136102
(document.head || document.documentElement).appendChild(scriptTag);
137103
});

0 commit comments

Comments
 (0)