chrome.runtime.onMessage.addListener не определен в скрипте содержимого

1

Я пытаюсь отправить сообщение на "Фон", а также получить обратно к скрипту содержимого. Изображение 174551

Изображение 174551

Это мой content_script.js

init();
function init() {
   function notifyBackgroundPage(event) {
        chrome.runtime.sendMessage(chromeExtID, {
            greeting: "Greeting from the content script"
        });
    }

    window.addEventListener("click", notifyBackgroundPage);

    // accept messages from background
    // below line addListener is undefined
    chrome.runtime.onMessage.addListener(function backgroundListener(request, sender, sendResponse) {
        console.log("BgExt.js says: " + request);
    });
  }

BgExt.js

define([], function GmailExt() {

    return {
        start: function () {

            chrome.runtime.onMessage.addListener(
                function (request, sender, sendResponse) {
                    console.log('inside response');
                    // to send back your response  to the current tab
                    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
                        chrome.tabs.sendMessage(tabs[0].id, {farewell: response}, function (response) {
                        });
                    });
                    return true;
                }
            );
        }
    };
});

Этот файл GmailExt загружается внутри all.js (в то время как all.js вставлен в index.html)

require([
    "base_host/chrome/Server",
    "kernel/Kernel",
    "hydra/version",
    "base_host/chrome/StartupConfig",
    "hydra/apps",
    "dojo/_base/Deferred",
    "talkto_util/common",
    "dojo/i18n!base_host/nls/CommonStrings",
    "base_host/chrome/BgExt",
    "dojo/domReady!"
], function (Server, Kernel, version, StartupConfig, apps, Deferred, talktoUtil, nls, BgExt) {
    document.getElementById("logoMessage").innerText = nls.simple_secure_text;
    var host = new Server();
    //if app and not extension
    chrome.browserAction && chrome.browserAction.onClicked.addListener(function() {
        Deferred.when(kernel.onInit(), function () {
            host.appManager.showShell();
        });
        return true;
    });

    BgExt.start();
});

client_base/хозяин/хром /index.html

<!DOCTYPE HTML>
<html>
<head>
    <!-- Background Page for Extension/app -->
    <title>Flock</title>
    <meta charset="UTF-8">
    <!--BUILD_REPLACE_BLOCK_START-->
    <!-- This block will be remove by build.
    All the files from libs/common/files.txt would be prepended to dojo.js -->
    <script type="text/javascript" src="../../../hydra/src/libs/common/dojoConfig.js"></script>
    <script type="text/javascript" src="../../../hydra/src/libs/common/underscore.js"></script>
    <script type="text/javascript" src="../../../hydra/src/libs/common/underscore-ext.js"></script>
    <!--BUILD_REPLACE_BLOCK_END-->
    <script type="text/javascript" src="../../../hydra/src/libs/dojotoolkit/dojo/dojo.js"></script>
    <script type="text/javascript" src="all.js"></script>
</head>
<body>

</body>
</html>

manifest.json

    {
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval' https://j.maxmind.com https://ssl.google-analytics.com https://flock-apps.flock.co https://flock-apps.flock-staging.co https://flock-apps.flock.com https://flock-apps.flock-staging.com; object-src 'self'",
  "minimum_chrome_version": "22",

  "options_page": "client_base/host/chrome/static/crx_browser_actions/index.html?app=preferences",
  "name": "__MSG_extName__",
  "description": "__MSG_extDescription__",
  "background": {
    "page": "client_base/host/chrome/index.html",
    "persistent": true
  },

  "browser_action": {
    "default_popup": "/gmail_ext/popup.html"
  },
  "web_accessible_resources": [
    "client_base/host/chrome/static/blank.gif",
    "gmail_ext/icons.png",
    "gmail_ext/jquery-3.2.1.min.js",
    "gmail_ext/gmail.js",
    "gmail_ext/content_script.js"
  ],
  "permissions": [
    "<all_urls>",
    "unlimitedStorage",
    "notifications",
    "idle",
    "background",
    "tabs",
    "activeTab"
  ],
  "optional_permissions": [
    "clipboardWrite"
  ],
  "externally_connectable": {
    "matches": [
      "https://*.google.com/*",
      "http://localhost/*",
    ]
  },
  "content_scripts": [
    {
      "matches": [
        "*://mail.google.com/*"
      ],
      "css": [
        "/gmail_ext/content_script.css"
      ],
      "js": [
        "/gmail_ext/loader.js"
      ],
      "run_at": "document_end"
    }
  ],
  "version": "1.0"
}
  • 1
    Консоль показывает, что ошибка происходит в VM2279, что означает, что код не является скриптом содержимого. Очевидно, что-то в вашем скрипте внедряет код в элемент скрипта, который выполняется в контексте страницы - он больше не является скриптом контента и, следовательно, не имеет доступа к chrome.runtime.onMessage.
  • 0
    @wOxxOm, вы были правы, но это только то, что моя функция инициализации вызывается из моей refresh function чтобы убедиться, что init function запускается только после того, как весь AJAX сделан.
Показать ещё 9 комментариев
Теги:
google-chrome-extension
dojo

1 ответ

0
Лучший ответ

Я починил это. Решение было с тех пор, как я использовал loader.js для загрузки моего скрипта контента и jquery через chrome.extension.getURL('/gmail_ext/content_script.js');

И использовали "content_script.js" как web_accessible_resources только loader.js имел доступ к хромированному объекту.

И да, проект Dojo, вы можете увидеть Рабочее расширение здесь: https://chrome.google.com/webstore/detail/flock-chat-for-teams-and/enfaahabcinohafeakbliimmoholjeip?hl=ru

Сейчас я использую события страницы, как показано здесь: https://developer.chrome.com/extensions/content_scripts#host-page-communication

В верхней части loader.js я добавил ниже:

function initPort() {
    var contentScriptPort,
        chromeExtID = "lddaepjihbpbfpegjhjnkffjmmoigphe";

    contentScriptPort = chrome.runtime.connect(chromeExtID);

    contentScriptPort.onMessage.addListener(function (message) {
        // Send data back to content script received from Background.
        window.postMessage({type: "FROM_BACKGROUND", emails: message}, "*");
    });

    window.addEventListener("message", function _postMessage(event) {
        // We only accept messages from ourselves
        if (event.source != window)
            return;

        if (event.data.type && (event.data.type == "FROM_PAGE")) {
            // console.log("Content script received: " + event.data.emails);
            contentScriptPort.postMessage(event.data);
        }
    }, false);
}

initPort();

Ещё вопросы

Сообщество Overcoder
Наверх
Меню