如何为 URL 中的给定哈希触发 Chrome 扩展程序、内容脚本? [英] How to trigger a Chrome extension, content-script for a given hash in the URL?

查看:11
本文介绍了如何为 URL 中的给定哈希触发 Chrome 扩展程序、内容脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的匹配方案:

"content_scripts" : [
 {
   "matches" : [
     "https://stackoverflow.com/questions#epic*"
   ],
   "js" : ["silly.js"]
 }
],

因此,如果用户访问某个网页(例如 https://stackoverflow.com/questions),则添加了 #epic 它将转到 https://stackoverflow.com/questions#epic 但会有 #epic 在 URL 的末尾,这将激活内容脚本 silly.js.

So if the user went to a webpage (like https://stackoverflow.com/questions) then added #epic it would go to https://stackoverflow.com/questions#epic but would have #epic on the end of the URL, which would activate the content script silly.js.

这是应该发生的事情,但这不起作用.

That's what's supposed to happen, but that doesn't work.

推荐答案

参见 内容脚本,匹配图案.
匹配模式不适用于 URL 的 fragment 部分.

要将内容脚本限制为给定的哈希值,请使用 include_globs 和/或 exclude_globs 属性.

To restrict a content script to a given hash, use the include_globs and/or exclude_globs properties.

例如,在这种情况下,您可以使用:

For example, in this case you might use:

"content_scripts" :     [ {
    "matches" :         [
        "*://stackoverflow.com/questions/*"
    ],
    "include_globs" :   ["*#epic*"],
    "js" :              ["silly.js"]
} ],

这篇关于如何为 URL 中的给定哈希触发 Chrome 扩展程序、内容脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆