-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw2.js
90 lines (77 loc) · 2.52 KB
/
w2.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function createWidget() {
console.log("createWidget")
const Widget = Object.create({
create(id) {
const style = `
.logFrame${ id } {
display: block;
width: 100%;
height: 100px;
border: none;
overflow-y: hidden;
overflow-x: hidden;
background-color: #0d1116 ;
border-radius: 5px;
color: #fff;
}
`
// snippet = document.currentScript.innerHTML
const wdg = document.createElement("div");
wdg.innerHTML = `
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>${ style }</style>
</head>
<div class=\"widgetContainer${id}\">
<iframe sandbox=\'allow-scripts\' id=\'sandboxed-${ id }\' class=\'logFrame${ id }\' title=\'Console Frame\'></iframe>
</div>
`;
wdg.script = document.createElement("script")
wdg.script.setAttribute('type', 'text/javascript');
wdg.appendChild(wdg.script)
return wdg;
}
});
const id = `js${Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)}`;
const myWidgetInstance = Widget.create(id);
document.body.innerHTML=`<div id= ${id} ></div>`;
document.getElementById(id).appendChild(myWidgetInstance);
console.log(id)
let ifr = document.getElementById(`sandboxed-${id}`)
ifr.srcdoc = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Widget</title>
<style>
/* Add your widget styles here */
.hello-widget {
color: #fff;
}
</style>
</head>
<body>
<div class="hello-widget">
<div id=\'log-${ id }\'></div>
</div>
<script>
(function() {
var logger = document.getElementById(\'log-${ id }\');
let fetchRes = fetch("https://type.fit/api/quotes")
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data[0]);
logger.innerHTML += 'Quote of the day :' + \'<h1>'\ + data[0].text + \'</h1>\';
});
})();
</script>
</body>
</html>
`
};
createWidget()