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";
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});
}
}

View file

@ -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);
}
}

View file

@ -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

View file

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