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

Commit ffcccbc

Browse files
committed
fix: monkey patch screen into globals
1 parent 694ffe7 commit ffcccbc

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"persistent": false
1616
},
1717
"web_accessible_resources": [
18-
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js"
18+
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js",
19+
"src/globals.js"
1920
],
2021
"content_scripts": [
2122
{

src/contentScript.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ chrome.runtime.onMessage.addListener((request) => {
2020
if (request.type == "getSuggestedQuery") {
2121
const { suggestedQuery } = getClosestQuery(currentElement, request.variant);
2222
if (suggestedQuery) {
23-
navigator.clipboard.writeText(suggestedQuery.toString()).then(
23+
navigator.clipboard.writeText(`screen.${suggestedQuery.toString()}`).then(
2424
() => {},
2525
() => {
2626
// eslint-disable-next-line no-console
@@ -77,13 +77,21 @@ function showElement(el) {
7777
}
7878

7979
window.showElement = showElement;
80+
function injectScript(scriptPath) {
81+
return new Promise((resolve) => {
82+
const scriptTag = document.createElement("script");
83+
scriptTag.src = chrome.runtime.getURL(scriptPath);
8084

81-
const scriptTag = document.createElement("script");
82-
scriptTag.src = chrome.runtime.getURL(
83-
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js"
84-
);
85+
scriptTag.onload = function onload() {
86+
resolve();
87+
this.remove();
88+
};
89+
(document.head || document.documentElement).appendChild(scriptTag);
90+
});
91+
}
8592

86-
scriptTag.onload = function onload() {
87-
this.remove();
88-
};
89-
(document.head || document.documentElement).appendChild(scriptTag);
93+
injectScript(
94+
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js"
95+
).then(() => {
96+
injectScript("src/globals.js");
97+
});

src/globals.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const Screen = Object.getPrototypeOf(window.screen);
2+
3+
for (const func of Object.keys(window.TestingLibraryDom.screen)) {
4+
Screen[func] = window.TestingLibraryDom.screen[func];
5+
}
6+
7+
window.fireEvent = window.TestingLibraryDom.fireEvent;

0 commit comments

Comments
 (0)