Skip to content

Commit 477f179

Browse files
committed
feat: queryDocumentSelector to return one element from an iframe or iframes parent
1 parent 71491b7 commit 477f179

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@ export function queryDocumentSelectorAll(selector) {
159159
}
160160
}
161161

162+
export function queryDocumentSelector(selector) {
163+
if (selector) {
164+
let selectors = [selector];
165+
if(selector.indexOf(',') !== -1){
166+
selectors = selector.split(',');
167+
}
168+
for (let selector of selectors){
169+
let el;
170+
if(selector.indexOf(';') !== -1) {
171+
let targetDocument;
172+
let [documentSelector, targetSelector] = selector.split(';');
173+
if (['parent', 'parentDocument'].includes(documentSelector))
174+
targetDocument = window.parent.document;
175+
else {
176+
let frame = document.querySelector(documentSelector);
177+
if (frame)
178+
targetDocument = frame.contentDocument;
179+
}
180+
if (targetDocument){
181+
if (targetSelector)
182+
el = targetDocument.querySelector(targetSelector);
183+
else
184+
if (targetDocument.clickedElement)
185+
el = [targetDocument.clickedElement];
186+
}
187+
}
188+
else
189+
el = document.querySelector(selector);
190+
if (el)
191+
return el
192+
}
193+
return;
194+
}
195+
}
196+
162197
// export function computeStyles(el, properties) {
163198
// let computed = window.getComputedStyle(el);
164199
// let result = {};

0 commit comments

Comments
 (0)