fished base communication between browser and chatterino

This commit is contained in:
fourtf 2018-04-12 01:17:25 +02:00
parent d3bbbafca7
commit b978977e7a
4 changed files with 56 additions and 29 deletions

View file

@ -10,41 +10,37 @@ const ignoredPages = {
const appName = "com.chatterino.chatterino"; const appName = "com.chatterino.chatterino";
let port; /// Connect to port
function matchUrl(url) { let port = null;
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 connectPort() { 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) { port.onMessage.addListener(function(msg) {
console.log(msg); console.log(msg);
}); });
port.onDisconnect.addListener(function() { port.onDisconnect.addListener(function() {
console.log("Disconnected"); console.log("port disconnected");
port = null;
}); });
} }
function selectChannel(channelName) { function getPort() {
console.log(channelName); 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.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => { chrome.tabs.get(activeInfo.tabId, (tab) => {
if (!tab) if (!tab)
@ -63,3 +59,30 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
matchUrl(changeInfo.url); 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});
}
}

View file

@ -114,12 +114,8 @@ void runNativeMessagingHost()
std::cin.read(b, size); std::cin.read(b, size);
*(b + size) = '\0'; *(b + size) = '\0';
nmm.writeByteArray("{\"a\":1}");
nmm.sendToGuiProcess(QByteArray(b, size)); nmm.sendToGuiProcess(QByteArray(b, size));
nmm.writeByteArray("{\"a\":2}");
free(b); free(b);
} }
} }

View file

@ -95,9 +95,7 @@ void NativeMessagingManager::openGuiMessageQueue()
void NativeMessagingManager::sendToGuiProcess(const QByteArray &array) void NativeMessagingManager::sendToGuiProcess(const QByteArray &array)
{ {
#ifdef BOOSTLIBS #ifdef BOOSTLIBS
writeByteArray("{\"b\": 1}");
ipc::message_queue messageQueue(ipc::open_or_create, "chatterino_gui", 100, MESSAGE_SIZE); ipc::message_queue messageQueue(ipc::open_or_create, "chatterino_gui", 100, MESSAGE_SIZE);
writeByteArray("{\"b\": 2}");
try { try {
messageQueue.try_send(array.data(), array.size(), 1); messageQueue.try_send(array.data(), array.size(), 1);
@ -122,13 +120,20 @@ void NativeMessagingManager::ReceiverThread::run()
messageQueue.receive(buf, MESSAGE_SIZE, retSize, priority); messageQueue.receive(buf, MESSAGE_SIZE, retSize, priority);
QString text = QString::fromUtf8(buf, retSize); QJsonDocument document = QJsonDocument::fromRawData(buf, retSize);
qDebug() << text;
this->handleMessage(document.object());
} catch (ipc::interprocess_exception &ex) { } catch (ipc::interprocess_exception &ex) {
qDebug() << "received from gui process:" << ex.what(); qDebug() << "received from gui process:" << ex.what();
} }
} }
#endif #endif
} }
void NativeMessagingManager::ReceiverThread::handleMessage(const QJsonObject &root)
{
// TODO: add code xD
}
} // namespace singletons } // namespace singletons
} // namespace chatterino } // namespace chatterino

View file

@ -16,6 +16,9 @@ public:
{ {
public: public:
void run() override; void run() override;
private:
void handleMessage(const QJsonObject &root);
}; };
void writeByteArray(QByteArray a); void writeByteArray(QByteArray a);