Skip to content

Commit efd2f5a

Browse files
fadmarinafr0l
authored andcommitted
Fetch screenshot and tree from url params
1 parent 60dd051 commit efd2f5a

1 file changed

Lines changed: 99 additions & 8 deletions

File tree

js/app.js

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class App extends React.Component {
2828
constructor(props) {
2929
super(props);
3030
this.state = {};
31+
32+
const urlParams = new URLSearchParams(window.location.search);
33+
this.screenshot = urlParams.get('screenshot');
34+
this.tree = urlParams.get('tree');
35+
36+
if (this.screenshot && this.tree) {
37+
const path = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'))
38+
window._da_address = window.location.origin + path;
39+
}
3140
}
3241

3342
refreshApp() {
@@ -39,11 +48,73 @@ class App extends React.Component {
3948
this.refreshApp();
4049
}
4150

42-
fetchScreenshot() {
51+
fetchOrientation(callback) {
4352
HTTP.get(ORIENTATION_ENDPOINT, (orientation) => {
44-
orientation = orientation.value;
45-
HTTP.get(SCREENSHOT_ENDPOINT, (base64EncodedImage) => {
46-
base64EncodedImage = base64EncodedImage.value;
53+
callback(orientation.value);
54+
})
55+
}
56+
57+
fetchStaticOrientation(callback) {
58+
callback(
59+
{
60+
"UIApplication_activeInterfaceOrientation":[1,"portrait"],
61+
"UIDevice":[0,"unknown"],
62+
"XCUIDevice":[0,"unknown"],
63+
"SpringBoard_XCUIApplication":[1,"portrait"],
64+
"AUT":[1,"portrait"],
65+
}
66+
)
67+
}
68+
69+
fetchDaScreenshot(callback) {
70+
HTTP.get(SCREENSHOT_ENDPOINT, (base64EncodedImage) => {
71+
callback(base64EncodedImage.value);
72+
});
73+
}
74+
75+
toDataUrl(src, callback, outputFormat) {
76+
const img = new Image();
77+
img.crossOrigin = 'Anonymous';
78+
img.onload = function() {
79+
const canvas = document.createElement('canvas');
80+
const ctx = canvas.getContext('2d');
81+
let dataURL;
82+
canvas.height = this.naturalHeight;
83+
canvas.width = this.naturalWidth;
84+
ctx.drawImage(this, 0, 0);
85+
dataURL = canvas.toDataURL(outputFormat);
86+
87+
callback(dataURL.slice('data:image/png;base64,'.length));
88+
};
89+
img.src = src;
90+
}
91+
92+
fetchStaticScreenshot(url, callback) {
93+
this.toDataUrl(
94+
url,
95+
callback,
96+
)
97+
}
98+
99+
defineScreenshot(callback) {
100+
if (this.screenshot == null) {
101+
return this.fetchDaScreenshot(callback)
102+
} else {
103+
return this.fetchStaticScreenshot(this.screenshot, callback)
104+
}
105+
}
106+
107+
defineOrientation(callback) {
108+
if (this.screenshot == null) {
109+
return this.fetchOrientation(callback)
110+
} else {
111+
return this.fetchStaticOrientation(callback)
112+
}
113+
}
114+
115+
fetchScreenshot() {
116+
this.defineOrientation((orientation) => {
117+
this.defineScreenshot((base64EncodedImage) => {
47118
ScreenshotFactory.createScreenshot(orientation, base64EncodedImage, (screenshot) => {
48119
this.setState({
49120
screenshot: screenshot,
@@ -53,14 +124,34 @@ class App extends React.Component {
53124
});
54125
}
55126

56-
fetchTree() {
127+
defineTree(callback) {
128+
    if (this.tree == null) {
129+
return this.fetchDaTree(callback)
130+
} else {
131+
return this.fetchStaticTree(this.tree, callback)
132+
}
133+
}
134+
135+
fetchStaticTree(url, callback) {
136+
HTTP.get(url, (treeInfo) => {
137+
callback(treeInfo);
138+
});
139+
}
140+
141+
fetchDaTree(callback) {
57142
HTTP.get(TREE_ENDPOINT, (treeInfo) => {
58-
// treeInfo = treeInfo.value;
143+
callback(treeInfo);
144+
});
145+
}
146+
147+
fetchTree() {
148+
this.defineTree((treeInfo) => {
59149
this.setState({
60150
rootNode: TreeNode.buildNode(treeInfo, new TreeContext()),
61151
});
62-
});
63-
}
152+
}
153+
);
154+
}
64155

65156
render() {
66157
return (

0 commit comments

Comments
 (0)