Skip to content
This repository was archived by the owner on Mar 11, 2019. It is now read-only.
This repository was archived by the owner on Mar 11, 2019. It is now read-only.

[Firefox][1.8.0], content-script's messageListener callback doesn't have access to window.* properties #167

@ferdibiflator

Description

@ferdibiflator

There is a problem with access to window.* properties inside message listener's callback in Firefox with kango 1.8.0. This problem doesn't occur in kango 1.7.9.

For example, if I do in content-script:

kango.addMessageListener('msg', function() {
    document.querySelectorAll('input');
});

and in background-script:

tab.dispatchMessage('msg');

I'll get a message from Firefox:

Error: Permission denied to access property "document"

I solved this problem by calling a callback in Promise:

// Custom message listener

'use strict';

function MessageListener() {
    this.addedListeners = new Map();
}

MessageListener.prototype.add = function (message, callback) {
    const isFirefox = typeof InstallTrigger !== 'undefined';
    let adaptedCallback = callback;

    if(isFirefox) {
        adaptedCallback = runAsync.bind(null, callback)
    }

    kango.addMessageListener(message, adaptedCallback)

    this.addedListeners.set(callback, adaptedCallback);
};

MessageListener.prototype.remove = function (message, callback) {
    const adaptedCallback = this.addedListeners.get(callback);

    kango.removeMessageListener(message, adaptedCallback);

    this.addedListeners.delete(callback);
};

module.exports = MessageListener;

function runAsync(callback, event) {
    new Promise(resolve => {
        callback(event);

        resolve();
    }).catch(e => console.log(e));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions