mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
fished base communication between browser and chatterino
This commit is contained in:
parent
d3bbbafca7
commit
b978977e7a
4 changed files with 56 additions and 29 deletions
|
@ -10,41 +10,37 @@ const ignoredPages = {
|
|||
|
||||
const appName = "com.chatterino.chatterino";
|
||||
|
||||
let port;
|
||||
/// Connect to port
|
||||
|
||||
function matchUrl(url) {
|
||||
if (!url)
|
||||
return;
|
||||
|
||||
const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9]+)\/?$/);
|
||||
|
||||
if (match) {
|
||||
const channelName = match[2];
|
||||
|
||||
if (!ignoredPages[channelName]) {
|
||||
selectChannel(channelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
let port = null;
|
||||
|
||||
function connectPort() {
|
||||
let port = chrome.runtime.connectNative("com.chatterino.chatterino");
|
||||
port = chrome.runtime.connectNative("com.chatterino.chatterino");
|
||||
console.log("port connected");
|
||||
|
||||
port.onMessage.addListener(function(msg) {
|
||||
console.log(msg);
|
||||
});
|
||||
port.onDisconnect.addListener(function() {
|
||||
console.log("Disconnected");
|
||||
console.log("port disconnected");
|
||||
|
||||
port = null;
|
||||
});
|
||||
}
|
||||
|
||||
function selectChannel(channelName) {
|
||||
console.log(channelName);
|
||||
function getPort() {
|
||||
if (port) {
|
||||
return port;
|
||||
} else {
|
||||
// TODO: add cooldown
|
||||
connectPort();
|
||||
|
||||
port.postMessage({channelName: channelName});
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
/// add listeners
|
||||
/// Tab listeners
|
||||
|
||||
chrome.tabs.onActivated.addListener((activeInfo) => {
|
||||
chrome.tabs.get(activeInfo.tabId, (tab) => {
|
||||
if (!tab)
|
||||
|
@ -63,3 +59,30 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
|||
|
||||
matchUrl(changeInfo.url);
|
||||
});
|
||||
|
||||
|
||||
/// Misc
|
||||
|
||||
function matchUrl(url) {
|
||||
if (!url)
|
||||
return;
|
||||
|
||||
const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9]+)\/?$/);
|
||||
|
||||
if (match) {
|
||||
const channelName = match[2];
|
||||
|
||||
if (!ignoredPages[channelName]) {
|
||||
selectChannel(channelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function selectChannel(channelName) {
|
||||
console.log("select" + channelName);
|
||||
|
||||
let port = getPort();
|
||||
if (port) {
|
||||
port.postMessage({action: "select", channelName: channelName});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,12 +114,8 @@ void runNativeMessagingHost()
|
|||
std::cin.read(b, size);
|
||||
*(b + size) = '\0';
|
||||
|
||||
nmm.writeByteArray("{\"a\":1}");
|
||||
|
||||
nmm.sendToGuiProcess(QByteArray(b, size));
|
||||
|
||||
nmm.writeByteArray("{\"a\":2}");
|
||||
|
||||
free(b);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,9 +95,7 @@ void NativeMessagingManager::openGuiMessageQueue()
|
|||
void NativeMessagingManager::sendToGuiProcess(const QByteArray &array)
|
||||
{
|
||||
#ifdef BOOSTLIBS
|
||||
writeByteArray("{\"b\": 1}");
|
||||
ipc::message_queue messageQueue(ipc::open_or_create, "chatterino_gui", 100, MESSAGE_SIZE);
|
||||
writeByteArray("{\"b\": 2}");
|
||||
|
||||
try {
|
||||
messageQueue.try_send(array.data(), array.size(), 1);
|
||||
|
@ -122,13 +120,20 @@ void NativeMessagingManager::ReceiverThread::run()
|
|||
|
||||
messageQueue.receive(buf, MESSAGE_SIZE, retSize, priority);
|
||||
|
||||
QString text = QString::fromUtf8(buf, retSize);
|
||||
qDebug() << text;
|
||||
QJsonDocument document = QJsonDocument::fromRawData(buf, retSize);
|
||||
|
||||
this->handleMessage(document.object());
|
||||
} catch (ipc::interprocess_exception &ex) {
|
||||
qDebug() << "received from gui process:" << ex.what();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void NativeMessagingManager::ReceiverThread::handleMessage(const QJsonObject &root)
|
||||
{
|
||||
// TODO: add code xD
|
||||
}
|
||||
|
||||
} // namespace singletons
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -16,6 +16,9 @@ public:
|
|||
{
|
||||
public:
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
void handleMessage(const QJsonObject &root);
|
||||
};
|
||||
|
||||
void writeByteArray(QByteArray a);
|
||||
|
|
Loading…
Reference in a new issue