-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
112 lines (104 loc) · 3.51 KB
/
background.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
// Chrome Extension ID: ljdobmomdgdljniojadhoplhkpialdid
// Firefox Extension ID: {91f05833-bab1-4fb1-b9e4-187091a4d75d}
var extensionId = bowser.firefox ? '{91f05833-bab1-4fb1-b9e4-187091a4d75d}' : 'ljdobmomdgdljniojadhoplhkpialdid';
/*
Periodically send a message to Katalon Recorder with a list of capabilities. If Katalon Recorder does not receive any message for 2 minutes, it will stop communicating with the plugin.
Message structure:
{
type: 'katalon_recorder_register',
payload: {
capabilities: [
{
id: <string: unique ID for capability>,
summary: <string: user-friendly name, e.g script format>,
type: <right now only 'export' is available>
}
]
}
}
*/
function register() {
chrome.runtime.sendMessage(
extensionId,
{
type: 'katalon_recorder_register',
payload: {
capabilities: [
{
id: 'plain-text', // unique ID for each capability
summary: 'Plain text', // user-friendly name
type: 'export' // for now only 'export' is available
},
{
id: 'json',
summary: 'JSON',
type: 'export'
}
]
}
}
);
}
register();
setInterval(register, 60 * 1000);
/*
Message sent from Katalon Recorder for the plugin to process.
{
type: <right now only 'katalon_recorder_export' is available>,
payload: {
capabilityId: <sent from plugin in katalon_recorder_register message, use this ID to differentiate between capabilites>
commands: [
{
command: <command name>,
target: <command target>,
value: <command value>
}
]
}
}
Response structure when message.type === 'katalon_recorder_export':
{
status: <boolean - whether the access was processed successfully>,
payload: {
content: <the exported script>,
extension: <extension when user wants to download the exported script>,
mimetype: <Katalon Recorder's code editor will use this mimetype to provide syntax highlighting>
}
}
*/
chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
if (message.type === 'katalon_recorder_export') {
var payload = message.payload;
var commands = payload.commands;
var content = '';
var extension = '';
var mimetype = '';
switch (payload.capabilityId) {
case 'plain-text':
for (var i = 0; i < commands.length; i++) {
var command = commands[i];
content += command.command + ' | ' + command.target + ' | ' + command.value + '\n';
}
extension = 'txt';
mimetype = 'text/plain';
break;
case 'json':
content = JSON.stringify(commands);
extension = 'json';
mimetype = 'application/ld-json';
break;
default:
content = 'Invalid capability ID';
extension = 'txt';
mimetype = 'text/plain';
}
sendResponse({
status: true,
payload: {
content: content,
extension: extension,
mimetype: mimetype
}
});
}
});