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