2
2
2byte2016-03-09 20:16:50
Google Chrome
2byte, 2016-03-09 20:16:50

Why doesn't messaging inside the background page work in Chrome extension?

From the popup and inline scripts in pages, chrome.runtime.sendMessage messages on the background page are visible, everything is ok. And sending inside the background page does not occur.
Manifest

"background": {
    "scripts": ["surfer.js", "eventPage.js"],
    "persistent": false
  },

surfer.js
'use strict';

/**
 * Surfer by profile pages
 *
 * @constructor
 */
function Surfer() {

}

Surfer.prototype.openWindows = function (urls) {

    var self = this;
    var urlCount = urls.length;
    var stoppedWork = false;

    // Listener status stopped
    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
        if (message.type == 'stopOpenWindows') {
            stoppedWork = true;

            sendResponse('OK');
        }
    });

    return new Promise(function (resolve, reject) {

        var currentUrl = 0;

        var urlOpen = function () {
            if (currentUrl < urlCount) {
                self.createTab(urls[currentUrl]).then(function (tab) {

                    chrome.runtime.sendMessage({type: 'newTab', tab: tab.id}, response => {
                        currentUrl++;
                        console.log('Send new tab', response);
                        urlOpen();
                    });

                }).catch(function (err) {
                    console.log(err);
                });
            } else {
                resolve();
            }
        };

        if (!stoppedWork) {
            urlOpen();
        } else{
            resolve();
        }

    });
};

eventPage.js
'use strict';

let siteTabId;
let tabs = [];

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {

    // Run script
    if (message.type == 'run') {

        siteTabId = message.tabId;

        chrome.tabs.executeScript(message.tabId, {
            file: 'popupExecutor.js'
        }, () => {
            console.log('Script runned.');

            sendResponse('OK');
        });

    }

    // Handler received users
    if (message.type == 'popupExecutor') {

        new Surfer()
            .openWindows(message.users.slice(2))
            .then(() => {
                console.log('Script is stopped!');
            }).catch(err => console.log(err));

        sendResponse('OK');
    }

    if (message.type == 'stopWork') {

        chrome.runtime.sendMessage({type: 'stopOpenWindows'}, response => {
            console.log('stepped');

            sendResponse('OK');
        });
    }

    console.log("Message eventPage", message);

});

Sending a message does not work if (message.type == 'stopWork') , it is listened to in new Surfer()
.openWindows(message.users.slice(2))
Maybe this is not possible at all?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question