This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
210 lines (176 loc) · 6.32 KB
/
app.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var app =
{
resultText: '',
adMobId: {},
initialize: function()
{
document.addEventListener('deviceready', app.onDeviceReady, false);
/**
* Get proper AdMob ad unit's IDs.
*
* https://github.com/floatinghotpot/cordova-admob-pro/wiki/00.-How-To-Use-with-PhoneGap-Build
*/
/**
* Windows 8 / Windows Phone 8
*/
app.adMobId = {
banner: 'ca-app-pub-2443694251751888/6171320055',
interstitial: 'ca-app-pub-2443694251751888/9124786451'
};
/**
* Android
*/
if(/(android)/i.test(navigator.userAgent)) {
app.adMobId = {
banner: 'ca-app-pub-2443694251751888/3915857654',
interstitial: 'ca-app-pub-2443694251751888/9403988055'
};
}
/**
* iOS
*/
if(/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {
app.adMobId = {
banner: 'ca-app-pub-2443694251751888/6310920852',
interstitial: 'ca-app-pub-2443694251751888/9264387253'
};
}
},
onDeviceReady: function()
{
document.addEventListener('menubutton', app.menuButtonHandler, false);
document.getElementById('scan').addEventListener('click', app.scan, false);
document.getElementById('encode').addEventListener('click', app.encode, false);
document.getElementById('browser').addEventListener('click', app.openInBrowser, false);
document.getElementById('clipboard').addEventListener('click', app.copyToClipboard, false);
app.toggleResultButtons('[none]');
document.getElementById('data-format').innerHTML = '[none]';
app.initAdMob();
app.scan();
},
menuButtonHandler: function()
{
/**
* Do nothing, when user presses system menu button. Used to handle small
* group of devices, that crashes Kode Reader app under Android 4.x or newer
* when user presses system menu button.
*
* See: http://stackoverflow.com/a/30968157/1469208 for details.
*/
//Do nothing...
},
toggleResultButtons: function(result)
{
app.resultText = result;
var resultString = (result.length > 33) ? result.substring(0, 32) + '...' : result;
document.getElementById('data-result').innerHTML = resultString;
if (app.validateURL(result))
{
document.getElementById('browser').removeAttribute('disabled');
}
else
{
document.getElementById('browser').setAttribute('disabled', 'diabled');
}
if ((result !== '[none]' && result !== ''))
{
document.getElementById('clipboard').removeAttribute('disabled');
}
else
{
document.getElementById('clipboard').setAttribute('disabled', 'diabled');
}
},
copyToClipboard: function()
{
var
text = app.resultText,
disabled = document.getElementById('clipboard').hasAttribute('disabled');
if (text === '[none]' || text === '' || disabled) return false;
/**
* https://build.phonegap.com/plugins/67
* https://github.com/VersoSolutions/CordovaClipboard/
*
* "Simplified" version of call as per http://stackoverflow.com/q/30758290/1469208
*/
window.plugins.copy(text);
// window.plugins.copy(text, function(){alert('Copied to clipboard!')}, function(){alert('Error copying code to clipboard! Sorry...')});
},
openInBrowser: function()
{
var
url = app.resultText,
disabled = document.getElementById('browser').hasAttribute('disabled');
if (url === '[none]' || url === '' || disabled) return false;
/**
* https://build.phonegap.com/plugins/233
* http://stackoverflow.com/a/17887465/1469208
* http://stackoverflow.com/a/20060846/1469208
* http://docs.phonegap.com/en/edge/cordova_inappbrowser_inappbrowser.md.html
*/
window.open(url, '_system');
},
scan: function()
{
navigator.vibrate(100);
var scanner = cordova.require("cordova/plugin/BarcodeScanner");
scanner.scan(function (result)
{
if(!result.cancelled)
{
document.getElementById('data-format').innerHTML = result.format;
app.toggleResultButtons(result.text);
}
}, function (error){alert("Scanning failed! Reason: ", error);});
return false;
},
encode: function()
{
var
result = window.prompt('Encode what?'),
scanner = cordova.require("cordova/plugin/BarcodeScanner");
/**
* Cancel encoding if user hit 'Cancel' button or entered an empty string.
*/
if (result === null || typeof result === 'object' || result === '') return false;
scanner.encode(scanner.Encode.TEXT_TYPE, result, function(success)
{
/**
* Do nothing...
*/
}, function(fail)
{
alert("Encoding failed! Reason: " + fail);
});
return false;
},
/**
* Used regular expression from:
*
* http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without
*
* http://stackoverflow.com/a/17726973/1469208
* http://stackoverflow.com/a/9284473/1469208
* http://stackoverflow.com/a/17714711/1469208
* http://stackoverflow.com/a/2015516/1469208
* http://stackoverflow.com/a/14582229/1469208
*/
validateURL: function(url)
{
var urlRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;
return urlRegex.test(url);
},
initAdMob: function()
{
/**
* https://github.com/floatinghotpot/cordova-admob-pro/wiki/00.-How-To-Use-with-PhoneGap-Build
*/
if (AdMob) {
AdMob.createBanner({
adId : app.adMobId.banner,
position : AdMob.AD_POSITION.BOTTOM_CENTER,
autoShow : true
});
}
}
};