-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexample.js
More file actions
155 lines (122 loc) · 5.32 KB
/
example.js
File metadata and controls
155 lines (122 loc) · 5.32 KB
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
/*
* example Receiver Plugin for OpenWebRx+
*
* License: MIT
* Copyright (c) 2023 Stanislav Lechev [0xAF], LZ2SLL
*/
// Disable CSS loading for this plugin
Plugins.example.no_css = true;
// remove the next line if you really want to use this plugin
throw ("This is the example plugin. It is not made for real world use.");
// Init function of the plugin
Plugins.example.init = function () {
// Check if utils plugin is loaded
if (!Plugins.isLoaded('utils', 0.1)) {
console.error('Example plugin depends on "utils >= 0.1".');
return false;
}
// Listen to profile change and print the new profile name to console.
// NOTE: you cannot manipulate the data in events, you will need to wrap the original
// function if you want to manipulate data.
$(document).on('event:profile_changed', function (e, data) {
console.log('profile change event: ' + data);
});
// Another events:
// OBSOLATE - event:owrx_initialized - called when OWRX is initialized
// better use Plugins.utils.on_ready() method
// Server events are triggered when server sends data over the WS
// All server events have suffix ':before' or ':after', based on the original function call.
// :before events are before the original function call,
// :after events are after the original function call.
// Some interesting server events:
// server:config - server configuration
// server:bookmarks - the bookmarks from server
// server:clients - clients number change
// server:profiles - sdr profiles
// server:features - supported features
// Modify an existing OWRX function with utils plugin.
// See utils.js for documentation on wrap method.
// This will wrap profile changing function
Plugins.utils.wrap_func(
// function to wrap around
'sdr_profile_changed',
// before callback, to be run before the original function
// orig = original function
// thisArg = thisArg for the original function
// args = the arguments for the original function
// If you call the original function here (in the before_cb), always return false,
// so the wrap_func() will not call it later again.
// example of calling the original function: orig.apply(thisArg, args);
function (orig, thisArg, args) {
console.log("Before callback for: " + orig.name);
// check if newly selected profile is the PMR profile
if ($('#openwebrx-sdr-profiles-listbox').find(':selected').text() === "[RTL] 446 PMR") {
// prevent changing to this profile
console.log('This profile is disabled by proxy function');
// restore the previous selected profile
$('#openwebrx-sdr-profiles-listbox').val(currentprofile.toString());
// return false to prevent execution of original function
return false;
}
// return true to allow execution of original function
return true;
},
// after callback, to be run after the original function,
// but only if the before callback returns true
// res = result of the original function, if any
function (res) {
console.log('profile changed.');
}
);
// this example will do the same (stop profile changing), but using another method
// replace the "onchange" handler of the profiles selectbox
// and call the original function "sdr_profile_changed"
$('#openwebrx-sdr-profiles-listbox')[0].onchange = function (e) {
// check the index of the selected profile (0 is the first profile in the list)
if (e.target.options.selectedIndex === 0) {
// prevent changing to this profile
console.log('This profile is disabled by onchange.');
// restore the previous profile
$('#openwebrx-sdr-profiles-listbox').val(currentprofile.toString());
e.preventDefault();
e.stopPropagation();
return false;
}
// otherwise, call the original function
sdr_profile_changed();
};
// this example will manipulate bookmarks data when the server sends the bookmarks
// We will wrap the bookmarks.replace_bookmarks() function, once OWRX is initialized.
// We cannot wrap the replace_bookmarks() function before the bookmarks object is created.
// So we wait for OWRX to initialize and then wrap the function.
// OBSOLATE - $(document).on('event:owrx_initialized', function () {
Plugins.utils.on_ready(function () {
// Call the wrap method of utils plugin
Plugins.utils.wrap_func(
// function to wrap
'replace_bookmarks',
// before callback
function (orig, thisArg, args) {
// check if the bookmarks are "server bookmarks"
if (args[1] === 'server') {
// check if we have array of bookmarks (will be empty if the profile has no bookmarks to show)
if (typeof (args[0]) === 'object' && args[0].length)
// replace the name of the first bookmark
args[0][0].name = 'replaced';
}
// now call the original function
orig.apply(thisArg, args);
// and return false, so the wrap_func() will not call the original for second time
return false;
},
// after callback
function (res) {
/* not executed because the before function returns false always */
},
// this is the object, where the replace_bookmarks() function should be found
bookmarks
);
});
// return true for plugin init()
return true;
} // end of init function