[英]How to ensure a chrome extension does not just work on local HTML files
我一直致力於Chrome擴展,其目的是從類似於https://secure.state.gov/的域中提取某些頁面的公共數據。 為了開發,我已經在本地保存了一些HTML文件,並且已經對它們進行了測試。 現在我已經開始工作了,我想嘗試使用一些實時公共頁面進行擴展。 但是,當我點擊該域中的擴展程序時,所有顯示的內容都是默認的Google擴展程序菜單,而不是我創建的工作本地頁面的popup.html。 除了本地HTML頁面之外的任何網頁上的此行為都是相同的。
在清單文件中,我嘗試在權限和content_scripts中添加“”。
"name": "APPNAME!",
"version": "1.0",
"description": "DESCRIPTION REDACTED!",
"permissions": ["activeTab", "declarativeContent", "storage" , "*://secure.state.gov/*", "<all_urls>"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"options_page": "formMenu.html",
"page_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
}
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"manifest_version": 2,
"content_security_policy": "script-src 'self' https://code.jquery.com https://cdnjs.cloudflare.com https://stackpath.bootstrapcdn.com https://use.fontawesome.com; object-src 'self'",
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["payload.js"],
"run_at": "document_end"
} ]
}
沒有錯誤消息。 我只是獲取默認擴展菜單(“此網站可以讀取和更改網站數據>”,“選項”,“從Chrome中刪除”,“在Chrome菜單中隱藏”,“管理擴展程序”,檢查彈出窗口)
要設置Chrome擴展程序以便使用本地HTML文件和特定域進行測試,請將其設置為background.js文件,如下所示:
'use strict'; chrome.runtime.onInstalled.addListener(function() { chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { chrome.declarativeContent.onPageChanged.addRules([{ conditions: [ new chrome.declarativeContent.PageStateMatcher({ pageUrl: { hostEquals: '' }, }), new chrome.declarativeContent.PageStateMatcher({ pageUrl: { hostContains: 'secure.state.gov' }, }) ], actions: [new chrome.declarativeContent.ShowPageAction()] }]); }); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.