0

下面是我的清单chrome.history在自定义扩展不确定

{ 
    "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是未定义的。 让我知道,如果我失去了一些东西。

回答

1

chrome.history无法在内容脚本中访问,您需要将该呼叫转移到background.js并使用类似Message Passing的内容传输数据。

Appendix

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

  • 延伸(使用getURL,inIncognitoContext,lastError,onRequest,sendRequest将)
  • I18N
  • 运行时(连接,getManifest,getURL,id,onConnect,onMessage,sendMessage)
  • 存储
+0

谢谢,工作。 – user1455116