【问题标题】:chrome.history is undefined in custom extensionchrome.history 在自定义扩展中未定义
【发布时间】:2016-12-14 14:15:04
【问题描述】:

下面是我的清单

{
    "manifest_version": 2,
    "name": "Print History",
    "version": "0.1",
    "permissions": ["history", "tabs"],
    "content_scripts": [
        {
        "matches": [
        "<all_urls>"
        ],
        "js": ["content.js"]
        }
    ],
    "browser_action":{
    },
    "background": {
        "scripts": ["background.js"]
    }
}

下面是我的后台js:

chrome.browserAction.onClicked.addListener(function(tab) {
  // Send a message to the active tab

    chrome.tabs.sendMessage(tab.id, {"message": "clicked_browser_action"});

});

下面是内容js:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "clicked_browser_action" ) {
        alert("hello");
     chrome.history.getVisits({"url": "www.facebook.com"},
        function (visits) {
            if (visits.length >= 0) {
                chrome.tabs.sendMessage(sender.tab.id, {
                    "message": "clicked_browser_action"
                });
            };
        });
    }
  }
);

警报打印在屏幕上,我在控制台中看到一个错误,显示 undefined.getVisits。出于某种原因, chrome.history 未定义。 如果我遗漏了什么,请告诉我。

【问题讨论】:

    标签: google-chrome google-chrome-extension google-chrome-devtools


    【解决方案1】:

    chrome.history 无法在内容脚本中访问,您需要将该调用移至background.js 并使用Message Passing 之类的东西来传输数据。

    Appendix

    内容脚本只能访问以下扩展api:

    • 扩展(getURL、inIncognitoContext、lastError、onRequest、sendRequest)
    • i18n
    • 运行时(connect、getManifest、getURL、id、onConnect、onMessage、sendMessage)
    • 存储

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      相关资源
      最近更新 更多