我正在尝试编写一个 URL 重定向 safari Web 扩展。
manifest.json
"declarative_net_request": {
"rule_resources": [
{
"id": "ruleset",
"enabled": true,
"path": "rules.json"
}
]
},
"permissions": [
"declarativeNetRequest",
"webNavigation"
]
}
规则.json
[
{
"id": 1,
"priority": 1,
"action": {
"type": "redirect",
"redirect": {
"regexSubstitution": "https://example.com/"
}
},
"condition": {
"regexFilter": "http://*/",
"resourceTypes": [
"main_frame"
]
}
}
]
期望 - 任何搜索都应重定向到 example.com
出现此错误
Non-fatal issue: Rule with id 1 is invalid. redirect is not a supported action type.
相同的代码作为 chrome 扩展在 chrome 中运行良好,仅在 safari 浏览器中给出错误。
safari不支持重定向吗?如果不是,我还有什么其他选择来实现它?
我正在研究这个,并在 6 个月前的 Webkit 博客 中找到了这篇注释。
在
中添加了对redirect
规则类型的支持, 这需要为 URL 的主机授予主机权限 正在重定向declarativeNetRequest
https://webkit.org/blog/12193/release-notes-for-safari-technology-preview-139/
我尝试在最新的 Beta iOS16 上进行重定向,并且得到了一些有用的东西...仍在测试...
最近的 ios 版本现在可以实现这一点。
redirect
规则自Safari 15.4起可用,并且regexSubsitution
已在Safari 17中添加。
要使用
redirect
规则,您需要拥有 "declarativeNetRequestWithHostAccess"
权限而不是 "declarativeNetRequest"
权限。 主机权限也应该是需要的,写为"host_permissions": ["<all_urls>"]
(尽管在撰写本文时,重定向可以在没有它的情况下工作)。
此外,
"http://*/"
不是正则表达式模式,而是全局模式,因此您可能需要将其更改为"url_filter"
而不是"regex_filter"
。
旁注:iPhone 模拟器可能不支持
redirect
规则(或者可能是整个 declarativeNetRequest)。我还没有设法让它在模拟器上运行。