-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #122 - introduced RPC connection and types
- Loading branch information
Showing
8 changed files
with
198 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This file is part of Chromium Browser Automation. | ||
* Copyright (C) 2020-present Manvel Saroyan | ||
* | ||
* Chromium Browser Automation is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Chromium Browser Automation is distributed in the hope that it will be | ||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Chromium Browser Automation. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @typedef {import("./types").RpcMessages} RpcMessages | ||
*/ | ||
|
||
// TODO: rename port. | ||
const port = browser.runtime.connect({name: "rpcPort"}); | ||
|
||
/** | ||
* Function for sending event to background page | ||
* Data: the path to the object (selector) or redirectionURL | ||
* evType: Type of the event (click, change, redirect) | ||
* newValue: newValue as example for changed value | ||
* @param {RpcMessages} message | ||
*/ | ||
function sendRpcMessage(message){ | ||
port.postMessage(message); | ||
} | ||
|
||
module.exports = {sendRpcMessage}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* This file is part of Chromium Browser Automation. | ||
* Copyright (C) 2020-present Manvel Saroyan | ||
* | ||
* Chromium Browser Automation is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Chromium Browser Automation is distributed in the hope that it will be | ||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Chromium Browser Automation. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @typedef {import("./types").RpcMessages} RpcMessages | ||
*/ | ||
|
||
/** | ||
* @callback RpcHandler | ||
* @param {RpcMessages} msg | ||
*/ | ||
|
||
/** | ||
* @type {RpcHandler[]} | ||
*/ | ||
const listeners = []; | ||
|
||
browser.runtime.onConnect.addListener((port) => { | ||
if (port.name !== "rpcPort") throw new Error("Unknown port."); | ||
port.onMessage.addListener(async(/** @type {RpcMessages} */ msg) => { | ||
for (const listener of listeners) { | ||
listener(msg); | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* Adds rpc listener. | ||
* @param {RpcHandler} handler - rpc listener handler. | ||
*/ | ||
function addRpcListener(handler) { | ||
listeners.push(handler); | ||
} | ||
|
||
/** | ||
* Removes rpc listener. | ||
* @param {RpcHandler} handler - rpc listener handler. | ||
*/ | ||
function removeRpcListener(handler) { | ||
const index = listeners.indexOf(handler); | ||
if (index !== -1) { | ||
listeners.splice(index, 1); | ||
} | ||
} | ||
|
||
module.exports = {addRpcListener, removeRpcListener}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* This file is part of Chromium Browser Automation. | ||
* Copyright (C) 2020-present Manvel Saroyan | ||
* | ||
* Chromium Browser Automation is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Chromium Browser Automation is distributed in the hope that it will be | ||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Chromium Browser Automation. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @typedef {import("../db/projects").ActionType} ActionType | ||
*/ | ||
|
||
/** | ||
* Record triggered event. | ||
* @typedef {object} RpcRecordedEvent | ||
* @property {"RecordedEvent"} msgType - Message ID. | ||
* @property {import("../db/projects").Action} action - action to record. | ||
*/ | ||
|
||
/** | ||
* Projects containing actions. | ||
* @typedef {object} RpcPlayProject | ||
* @property {"PlayProject"} msgType - Message ID. | ||
* @property {import("../db/projects").Action[]} actions - Injectable actions. | ||
* @property {string} repeatTimes - Repeat times. | ||
* @property {string} id - Unique Identifier. | ||
*/ | ||
|
||
/** | ||
* @typedef {RpcRecordedEvent|RpcPlayProject} RpcMessages | ||
*/ | ||
|
||
module.exports = {}; // Unless specified types are not being imported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters