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
|
module.exports = function (context) {
var deferred = context.requireCordovaModule('q').defer(),
fs = context.requireCordovaModule('fs'),
path = context.requireCordovaModule('path'),
projectRoot = context.opts.projectRoot;
// While on AppBuilder this may work, the Cordova CLI doesn't like it
// (or at least not all versions of it).
var appXaml = path.join(projectRoot, "App.xaml.cs");
var mainPageXaml = path.join(projectRoot, "MainPage.xaml.cs");
try {
fs.statSync(appXaml);
fs.statSync(mainPageXaml);
} catch (err) {
appXaml = path.join(projectRoot, "platforms", "wp8", "App.xaml.cs");
mainPageXaml = path.join(projectRoot, "platforms", "wp8", "MainPage.xaml.cs");
try {
fs.statSync(appXaml);
fs.statSync(mainPageXaml);
} catch (err2) {
console.error("Custom URL Scheme plugin couldn't find your App's xaml file! Try to adjust the file manually according to the 'add-uri-mapper.js' hook.");
return;
}
}
fs.readFile(appXaml, 'utf8', function (err,data) {
if (err) {
console.error("Error while configuring the Custom URL Scheme: " + err);
deferred.reject(err);
return;
}
var result = data.replace(/^(\s*?)(RootFrame.NavigationFailed\s*?\+=\s*?RootFrame_NavigationFailed;)/gm,
"$1$2\n\n$1// Assign the URI-mapper class to the application frame\n$1RootFrame.UriMapper = new CompositeUriMapper();");
fs.writeFile(appXaml, result, 'utf8', function (err) {
if (err){
deferred.reject(err);
} else{
deferred.resolve();
}
});
});
fs.readFile(mainPageXaml, 'utf8', function (err,data) {
if (err) {
console.error("Error while configuring the Custom URL Scheme: " + err);
deferred.reject(err);
return;
}
// first add a line to refer to a new method
var result = data.replace(/^(\s*?)(this.CordovaView.Loaded\s*?\+=\s*?CordovaView_Loaded;)/gm,
"$1$2\n\n$1// Wire a handler so we can check for our custom scheme\n$1this.CordovaView.Browser.LoadCompleted += CordovaBrowser_LoadCompleted;");
// now add that new method
result = result.replace(/^(\s*?)(\/\/ Constructor)/gm,
"$1void CordovaBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) {\n"+
"$1\tif (CompositeUriMapper.launchUrl != null) {\n" +
"$1\t\tstring handleOpenURL = string.Format(\"(function() {{ document.addEventListener('deviceready', function() {{ if (typeof handleOpenURL === 'function') {{ handleOpenURL(\\\"{0}\\\"); }} }}); }})()\", CompositeUriMapper.launchUrl);\n" +
"$1\t\ttry {\n" +
"$1\t\t\tthis.CordovaView.CordovaBrowser.InvokeScript(\"eval\", new string[] { handleOpenURL });\n" +
"$1\t\t} catch (Exception) {}\n" +
"$1\t\tCompositeUriMapper.launchUrl = null;\n" +
"$1\t}\n" +
"$1\tthis.CordovaView.Browser.LoadCompleted -= CordovaBrowser_LoadCompleted;\n" +
"$1}\n\n" +
"$1$2");
fs.writeFile(mainPageXaml, result, 'utf8', function (err) {
if (err){
deferred.reject(err);
} else{
deferred.resolve();
}
});
});
return deferred.promise;
}
|