更新时间:2023-12-05 18:46:34
As detailed in the documentation you cannot use chrome.*
API within content scripts except for some chrome.extension.*
methods.
However, this doesn't really limit you as you can use messaging to call your content script from your background page. For example;
background.html
<script type="application/javascript">
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.getSelected(function (tab) {
chrome.tabs.sendRequest(tab.id, {title: 'new page title'}, function (response) {});
});
});
</script>
changetitle.js
chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
document.title = request.title;
});
You will of course require the tabs permission in order to use this technique.