[英]Hide pageAction with declarativeContent in Google Chrome extension API
這是我第一次制作Chrome擴展程序,老實說,我對JS不太熟悉。
persistent: false
, declarativeContent
和pageStateMatcher
API調用和權限,而不是使用選項卡和/或在原始JS上完全pageStateMatcher
如果可以的話)。 當URL包含除二級域名以外的任何內容時,我需要顯示pageAction
圖標,但co.uk之類的外來pageAction
除外:
☐google.com— 2lvl域
☐bbc.co.uk—等同於2lvl域的異國3lvl域
☑images.google.com— 3lvl域
☑google.com/ search-具有路徑的2lvl域
☑news.bbc.co.uk—等同於3lvl域的異國4lvl域
☑bbc.co.uk/ article —具有路徑的等效3lvl域等於具有路徑的2lvl域
我需要為上述情況的相反方向隱藏pageAction
。
pageAction
進行的操作來通過修剪除第二級域以外的任何內容來更新URL。 我僅在檢測部分工作,打算在完成此部分后添加URL更新功能。
manifest.json
{
// Required
"manifest_version": 2,
"name": "Website Home Button",
"version": "0.1",
// Recommended
"description": "Icon made by Freepik from www.flaticon.com is licensed by CC BY 3.0",
"icons": {
"48": "icon48.png",
"128": "icon128.png"
},
// PageAction
"page_action": {
"default_icon": {
"19": "icon19.png",
"38": "icon38.png"
},
"default_title": "Website Home Button" //Tooltip
},
// Optional
"author": "Murad 'Erquint' Beybalaev",
"background": {
"scripts": ["background.js"],
// Recommended
"persistent": false
},
"minimum_chrome_version": "18",
"permissions": ["declarativeContent"]
}
background.js
// Update the declarative rules on install or upgrade.
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
// URL is not a 2lvl domain...
new chrome.declarativeContent.PageStateMatcher(
{
pageUrl: {urlMatches: "[^.]+?\..+?(\.|\/).*"}
}
),
//Hostname is not a 3lvl .uk domain
!(new chrome.declarativeContent.PageStateMatcher(
{
pageUrl: {hostSuffix: ".uk"}
}
))],
// ... show the page action.
actions: [new chrome.declarativeContent.ShowPageAction() ]
}]);
});
//[^.]+?\..+?(\.(?!uk)|\/).*
//(?<=\.)(.+?\..+)
在底部,您可以看到我將用於檢測和修整的正則表達式,但是pageStateMatcher
使用RE2進行正則表達式,並且它不支持環視(整個API對我來說適得其反),所以我決定使用hostSuffix
條件結合在一起,但為了上帝的愛,我不明白如何在這些條件下工作。
如果要遵循RE2約束,我需要的邏輯是(偽代碼)
if(
//URL contains more than just a 2nd-level domain
urlMatchRegExp("[^.]+?\..+?(\.|\/).*")
//AND URL isn't an exotic .uk 3rd-level domain.
&& !urlMatchRegExp("([^.]+?\.){2}uk"))
{pageAction.show}
else
{pageAction.hide}
我如何嵌套/ &&條件?
我該怎么辦! 條件?
條件或事件似乎不是布爾值,因此邏輯運算符似乎對它們不起作用。
我應該放棄API並使用JS RegExp嗎?
是否可以在沒有制表符權限的情況下更新URL?
我很茫然。
任何指導將不勝感激。
聚苯乙烯
我只是嘗試將此規則集與功能更強大的RegExp結合使用,但仍然沒有環顧四周:
[{
conditions: [
//URL is not a 2lvl domain...
new chrome.declarativeContent.PageStateMatcher(
{
pageUrl: {urlMatches: "^[^.]+?\..+?(\.[^uk]|\/).*$"}
}
)],
// ... show the page action.
actions: [new chrome.declarativeContent.ShowPageAction() ]
},{
conditions: [
//URL is not a 2lvl domain...
new chrome.declarativeContent.PageStateMatcher(
{
pageUrl: {urlMatches: "^[^.]+?\.[^\.|\/]+?(\.uk)??$"}
}
)],
// ... show the page action.
actions: [new chrome.declarativeContent.HidePageAction() ]
}]
但是事實證明沒有chrome.declarativeContent.HidePageAction()
。
不滿足條件時是否應該自動隱藏?
我們試試吧。
嗯,事實並非如此。
我正在嘗試上面的無隱藏規則,並且在任一規則上都顯示
example.com
www.example.com
example.com/index.html
www.example.com/index.html
而不管。
到處都是永久的。
盡管沒有隱藏規則,調試中也沒有錯誤。
PPS Kinda按照Google的模板重寫了整個過程:
background.js
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {urlMatches: "^[^.]+?\..+?(\.[^uk]|\/).*$"},
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
仍然得到所有可能的假陽性。
聚苯硫醚
RE2太爛了!
我將嘗試使用更多JS的tabs
。
RE2被證明是功能失調的災難。
某些Chrome API被證明是適得其反。
使用tabs
權限可以提高JS的靈活性,並且與純API(+ RE2)相比,它實際上可以工作。
無論如何都解決了環視問題-它們是“常規”正則表達式的有力補充,但強大的功能帶來了巨大的責任。
基本擴展功能已經到位。
計划“異國情調”域名和一些令人眼花dy亂的調整。
感謝@Xan提供的信息和建議。
如果有人對這個小項目感到好奇,可以在https://bitbucket.org/Erquint/website_home_button_extension_for_chrome中查看
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.