blob: 52fec8f6f1c122b7250d72e4575ddbf1f4d9406f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
export function getMetaValue(name) {
const element = findElement(document.head, `meta[name="${name}"]`)
if (element) {
return element.getAttribute("content")
}
}
export function findElements(root, selector) {
if (typeof root == "string") {
selector = root
root = document
}
const elements = root.querySelectorAll(selector)
return toArray(elements)
}
export function findElement(root, selector) {
if (typeof root == "string") {
selector = root
root = document
}
return root.querySelector(selector)
}
export function dispatchEvent(element, type, eventInit = {}) {
const { bubbles, cancelable, detail } = eventInit
const event = document.createEvent("Event")
event.initEvent(type, bubbles || true, cancelable || true)
event.detail = detail || {}
element.dispatchEvent(event)
return event
}
export function toArray(value) {
if (Array.isArray(value)) {
return value
} else if (Array.from) {
return Array.from(value)
} else {
return [].slice.call(value)
}
}
|