forked from wix-incubator/restaurants-userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-standalone-link.user.js
86 lines (75 loc) · 3.7 KB
/
setup-standalone-link.user.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
// ==UserScript==
// @version 1.0.0
// @author Wix Restaurants
// @description Sets all that is needed to open the restaurant's online ordering in standalone mode using 'alias'.
// @name Setup Standalone Link
// @match http://alpha.openrest.com/*
// @match https://restaurants.wix.com/*
// @grant GM_xmlhttpRequest
// @grant GM_openInTab
// ==/UserScript==
(function() {
'use strict';
const request = (params) => {
return fetch('https://api.openrest.com/v1.1', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
}).then(r => r.json()).then((res) => {
if (res.error) {
return Promise.reject(new Error(JSON.stringify(res)));
}
return res;
});
};
function authenticate() {
const appId = unsafeWindow.window.WixInstance.getAppId();
const instance = unsafeWindow.WixInstance.getInstance();
return fetch('https://auth.openrest.com/v1.0', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({type:'wix.loginInstance', instance, appKey:appId})
}).then(r => r.json()).then(res => {
return res.value;
});
}
if ((unsafeWindow.full) && (unsafeWindow.full.restaurant) && (unsafeWindow.full.restaurant.id) &&
(unsafeWindow.Wix) && (unsafeWindow.WixInstance.getPermissions() === 'owner') &&
(unsafeWindow.WixInstance.getAppId() === '13e8d036-5516-6104-b456-c8466db39542' /* orders */)) {
const {locale, title, id} = unsafeWindow.full.restaurant;
let componentInfo = null;
unsafeWindow.Wix.getComponentInfo(function(e) {
componentInfo = e;
});
let accessToken = null;
authenticate().then(res => accessToken = res.accessToken);
const button = $("<div style='position:fixed;z-index:10000000;top:0px;left:0px;width:20px;height:20px;background:green;text-align:center;line-height:20px;color:white;box-shadow:1px 1px 1px rgba(0,0,0,0.5);cursor:pointer;border-radius:50%'>M</div>").appendTo($(unsafeWindow.body));
button.on('click', () => {
const alias = unsafeWindow.prompt("Please enter alias for restaurant '" + title[locale]+ "':","");
if (alias) {
request({type: 'get_organization', organizationId: id}).then(res => {
const organization = res.value;
organization.externalIds = organization.externalIds || {};
organization.externalIds['com.wix.styles.appId'] = unsafeWindow.window.WixInstance.getAppId();
organization.externalIds['com.wix.styles.instanceId'] = unsafeWindow.WixInstance.getInstanceId();
organization.externalIds['com.wix.styles.compId'] = componentInfo.compId;
organization.externalIds['com.wix.styles.pageId'] = componentInfo.pageId;
return request({type:'set_organization', organization, accessToken});
}).then(res => {
return request({type:'set_app_mapping', appId:{platform:'com.openrest', id:alias}, organizationId:id, accessToken});
}).then(res => {
GM_openInTab('https://restaurants.wix.com/?type=wixorders.client.mobile.standalone&restaurantAlias='+alias, {active:true});
}).catch(e => {
alert(e.toString());
});
}
});
}
// Your code here...
})();