I'm trying to write a simple parental control app for my university project, but I'm a newbie in browser addons. I want to use Chrome addon to send hosts viewed by the user in real-time to Qt app, which will analyze the user behavior. The problem is, that sometimes chrome's sending a correct host, another time it's sending trash with an empty string or enormously long message, which my Qt app filter. But these 'wrong' messages are sent in an endless loop, and to make it working again, I have to restart extension or chrome or even whole PC.
Chrome addon manifest:
{
"name": "AM Chrome addon",
"version": "0.7",
"description": "Get your activity supervised!",
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"permissions": [
"tabs",
"nativeMessaging",
"background"
],
"manifest_version": 2
}
Addon background.js file:
var current = undefined;
var port = null;
tryConnectagain();
function tryConnectagain() {
port = chrome.runtime.connectNative('<Native host app-name>');
port.onDisconnect.addListener(onDisconnect);
}
function onDisconnect() {
port = null;
console.log("last error: ", chrome.runtime.lastError.message);
setTimeout(tryConnectagain, 1000);
}
function sendMessageToNativeApp(message) {
if (port != null) port.postMessage({ message: message });
}
function newUrl(u) {
if (u != undefined && !u.includes(current) && !u.includes("chrome-extension://") && u.includes('.')) {
var u = new URL(u);
var domain = u.hostname.replace("www.", "");
if (domain != current) {
current = domain;
sendMessageToNativeApp(current);
console.log(current);
}
}
else if (current != "NotURL") {
current = "NotURL";
sendMessageToNativeApp(current);
console.log(current);
}
}
// Here I'm trying to intercept all URL change situations
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
if (tab.active && tab.highlighted) newUrl(tab.url);
});
});
chrome.tabs.onAttached.addListener(function (tabId, attachInfo) {
chrome.tabs.get(tabId, function (tab) {
if (tab.active && tab.highlighted) newUrl(tab.url);
});
});
chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
chrome.tabs.get(addedTabId, function (tab) {
if (tab.active && tab.highlighted) newUrl(tab.url);
});
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url && tab.active && tab.highlighted) newUrl(changeInfo.url);
});
chrome.windows.onFocusChanged.addListener(function (windowId) {
if (windowId > -1) {
var getInfo = { populate: true, windowTypes: ['normal'] };
chrome.windows.getLastFocused(getInfo, function (window) {
for (var t = 0; t < window.tabs.length; t++) {
if (window.tabs[t].active && window.tabs[t].highlighted) {
newUrl(window.tabs[t].url);
break;
}
}
})
}
});
Native host app manifest:
{
"name": "<Native host app-name>",
"description": "Hostname Identifier",
"path": "<Hostname app Path>",
"type": "stdio",
"allowed_origins": [
"chrome-extension://<extension-ID>/"
]
}
And a fragment of c++ qt code that receive data from addon: addonmessagereceiver.h:
#ifndef ADDONMESSAGERECEIVER_H
#define ADDONMESSAGERECEIVER_H
#include <qthread.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <iostream>
#include <string>
class AddonMessageReceiver : public QThread
{
Q_OBJECT
public:
void run();
signals:
void UpdateMessage(const QString &);
};
#endif // ADDONMESSAGERECEIVER_H
addonmessagereceiver.cpp:
#include "addonmessagereceiver.h"
#include <qdebug.h>
using namespace std;
void AddonMessageReceiver::run()
{
do{
char nextMessageLen[4];
cin.read(nextMessageLen, 4);
unsigned long int messageLength = *reinterpret_cast<unsigned long int *>(nextMessageLen);
qDebug() << messageLength << static_cast<int>(nextMessageLen[0]) << static_cast<int>(nextMessageLen[1]) << static_cast<int>(nextMessageLen[2]) << static_cast<int>(nextMessageLen[3]);
if(messageLength<1024 && messageLength>1)
{
char *incomingMessage = new char[messageLength+1];
memset(incomingMessage,'\0',messageLength+1);
cin.read(incomingMessage, messageLength);
QString message = QString::fromLatin1(incomingMessage);
delete[] incomingMessage;
qDebug() << messageLength << message;
if(message.length()>5)
{
QJsonDocument json = QJsonDocument::fromJson(message.toLatin1());
QJsonObject obj = json.object();
QString host = obj.value("message").toString();
emit UpdateMessage(host);
}
}
QThread::msleep(100);
}while(true);
}
Example of qDebug wrong nextMessageLen in loop:
And an example of good input that turns into wrong in a loop:
Can you please tell me what is going on with that extension or chrome, or what I f... up with native app? Thank you for your answer.