我正在尝试为我的大学项目编写一个简单的家长控制应用程序,但我是浏览器插件的新手。我想使用Chrome插件将用户实时查看的主机发送到Qt应用程序,Qt应用程序将分析用户行为。问题是,有时 chrome 发送正确的主机,有时它发送带有空字符串或非常长的消息的垃圾,这是我的 Qt 应用程序过滤的。但这些“错误”消息会无限循环地发送,为了使其再次工作,我必须重新启动扩展程序或 chrome 甚至整个电脑。
Chrome 插件清单:
{
"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
}
插件background.js文件:
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;
}
}
})
}
});
本机主机应用程序清单:
{
"name": "<Native host app-name>",
"description": "Hostname Identifier",
"path": "<Hostname app Path>",
"type": "stdio",
"allowed_origins": [
"chrome-extension://<extension-ID>/"
]
}
以及从插件接收数据的 C++ qt 代码片段:
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);
}
qDebug 循环中错误的 nextMessageLen 示例:
一个好的输入在循环中变成错误的例子:
您能告诉我该扩展程序或 chrome 发生了什么,或者我对本机应用程序有什么问题吗?谢谢您的回答。