[英]Chrome Extension - Not working in the newly opened tab
我正在開發一個 chrome 擴展,它在除新選項卡之外的所有場景中都運行良好。
即,該擴展程序僅在為例如打開網站時才有效。 stackoverflow.com。 當我按 ctrl+t 並單擊我的擴展程序圖標時,它不起作用。
我做錯了什么? 還是瀏覽器行為?
我已經添加了我的代碼供您參考。
顯現
{
"manifest_version": 2,
"background": {
"scripts": ["scripts/background.js"],
"persistent": false
},
"content_scripts":[{
"matches" : ["<all_urls>"],
"js": ["scripts/jquery-2.1.0-min.js", "scripts/init.js"],
"run_at": "document_end"
}],
"permissions": [
"storage", "activeTab", "http://*/*", "https://*/*"
],
"browser_action": {
"default_icon": "images/plugin-icon-24.png"
},
"web_accessible_resources": [
"*.html",
"images/*.gif",
"images/*.png"
]
}
init.js
chrome.storage.sync.get('logged_in', function(status){
if(status.logged_in){
chrome.runtime.sendMessage('LOGGED_IN');
} else {
chrome.runtime.sendMessage('NOT_LOGGED_IN');
}
});
背景.js
var add_resource = function(){
chrome.tabs.executeScript({
file: 'scripts/plugin.js'
});
chrome.tabs.insertCSS({
file: 'styles/plugin.css'
});
};
chrome.runtime.onMessage.addListener(function(message){
alert(message);
/*This alerts comes even in the newly opened tab.
But the script is not getting executed.*/
if(message == 'LOGGED_IN'){
add_resource();
} else {
chrome.browserAction.onClicked.addListener(function(tab){
add_resource();
});
}
});
嘗試將下面的代碼添加到您的 manifest.json。
"chrome_url_overrides": {
"newtab": "blank.html"
}
blank.html: (創建你自己的版本)
<html>
<head>
<title>Blank New Tab</title>
<style>
div {
color: #cccccc;
vertical-align: 50%;
text-align: center;
font-family: sans-serif;
font-size: 300%;
}
</style>
</head>
<body>
<div style="height:40%"></div>
<div>Blank New Tab™</div>
</body>
</html>
在“權限”中添加“chrome://*/*” (用於新標簽頁),然后打開 chrome 並轉到chrome://flags/#extensions-on-chrome-urls並啟用此設置。
問題可能在於您的清單權限。
新標簽頁不使用http://
而是chrome://newtab
,因此您可能需要將其添加到您的權限中才能使您的擴展程序正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.