Несколько собственных приложений запускаются путем соединения с собственным обменом сообщениями при перезагрузке расширения - PullRequest
0 голосов
/ 04 октября 2019

В расширении chrome я использую собственный обмен сообщениями для вызова локального приложения. Но я обнаружил проблему, что каждый раз, когда я перезагружаю расширение, создается впечатление, что для приложения создается новый процесс. Согласно документации, приложение будет закрыто, если port отключен или страница закрыта. Означает ли это, что расширение перезагрузки не закроет страницу background? Как я могу решить эту проблему? Также я не могу найти свой локальный процесс приложения в диспетчере задач Chrome.

// background.js

var port = null;
connectToNativeHost();

// Receive message from other js
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log("background recieved message from " + sender.url + JSON.stringify(request));
        parseMessage(request);
    }
);

//onNativeDisconnect
function onDisconnected()
{
    console.log(chrome.runtime.lastError);
    console.log('disconnected from native app.');
    port = null;
}

// Receive message from native app
function onNativeMessage(message)
{
    console.log('recieved message from native app: ' + JSON.stringify(message));
}

//connect to native host and get the communicatetion port
function connectToNativeHost()
{
    var nativeHostName = 'com.group_project.time_tracker';
    port = chrome.runtime.connectNative(nativeHostName);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
    console.log("connected");
}

// Send message to native app
function sendMessage(message)
{
    port.postMessage(message);
    console.log('send messsage to native app: ' + JSON.stringify(message));
}
...