Skip to content

Commit c9a670a

Browse files
committed
add utils
1 parent 2a7d206 commit c9a670a

File tree

1 file changed

+74
-20
lines changed

1 file changed

+74
-20
lines changed

src/index.js

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export function getParentFromElement(element, parent_class, attributes) {
32
if (parent_class) {
43
if (element.classList.contains(parent_class)) {
@@ -235,29 +234,84 @@ export function getElementPath(element, returnContext) {
235234
// path.unshift(cssPath(iframeElement))
236235
// }
237236
}
238-
function logger(level = "all") {
239-
if(!['all', 'error', 'warn', 'log','off'].includes(level))
240-
throw new Error('level must be one of all, error, warn, log or off')
241-
return {
242-
error: function(msg) {
243-
// if (compoentToLoad.includes(comName))
244-
if (['all', 'error'].includes(level))
245-
console.error(msg)
246-
},
247-
warn: function(msg) {
248-
// if (compoentToLoad.includes(comName))
249-
if (['all', 'error', 'warn'].includes(level))
250-
console.warn(msg)
251-
},
252-
log: function(msg) {
253-
// if (compoentToLoad.includes(comName))
254-
if (['all', 'error', 'warn', 'log'].includes(level))
255-
console.log(msg)
256-
},
237+
export function logger(level = "all") {
238+
if (!['all', 'error', 'warn', 'log', 'off'].includes(level))
239+
throw new Error('level must be one of all, error, warn, log or off')
240+
return {
241+
error: function(msg) {
242+
// if (compoentToLoad.includes(comName))
243+
if (['all', 'error'].includes(level))
244+
console.error.apply(console, arguments)
245+
},
246+
warn: function(msg) {
247+
// if (compoentToLoad.includes(comName))
248+
if (['all', 'error', 'warn'].includes(level))
249+
console.warn.apply(console, arguments)
250+
},
251+
log: function() {
252+
// if (compoentToLoad.includes(comName))
253+
if (['all', 'error', 'warn', 'log'].includes(level))
254+
console.log.apply(console, arguments)
255+
},
256+
}
257+
258+
}
259+
export async function waitForLoad(doc) {
260+
261+
if (doc.contentDocument.readyState === 'loading') {
262+
try {
263+
await new Promise((resolve, reject) => {
264+
doc.contentWindow.addEventListener('load', (e) => resolve())
265+
});
257266
}
267+
catch (err) {
268+
console.error('iframe can not be loaded')
269+
}
270+
// this.observerElements(doc.contentWindow)
271+
// doc.contentWindow.observedByCCAttributes = true;
272+
}
273+
}
274+
275+
//export function frameQuerySelector(comSelector) {
276+
// let [canvasSelector, selector] = comSelector.split(';');
277+
// let canvas = this.querySelector(canvasSelector);
278+
// if (!canvas)
279+
// return null;
280+
281+
282+
// return canvas.contentWindow.document.querySelector(selector);
283+
// }
284+
285+
export function getComplexSelector(node, comSelector) {
286+
let selectors = comSelector.split(';');
287+
let canvas = node;
288+
for (let i = 0; i < selectors.length - 1; i++) {
289+
canvas = canvas.querySelector(selectors[i]);
290+
if (!canvas)
291+
return [];
292+
293+
}
294+
return [this, [selectors.length - 1]]
295+
}
296+
297+
export function frameQuerySelector(comSelector) {
298+
let [canvas, selector] = getComplexSelector(this, comSelector)
299+
if (canvas)
300+
return canvas.contentWindow.document.querySelector(selector);
301+
return null;
258302

259303
}
260304

305+
export function frameQuerySelectorAll(comSelector) {
306+
let [canvas, selector] = getComplexSelector(this, comSelector)
307+
if (canvas)
308+
return canvas.contentWindow.document.querySelectorAll(selector);
309+
return [];
310+
311+
312+
}
313+
314+
261315

262316
export default {
263317
getElementPath,

0 commit comments

Comments
 (0)