以下 Chrome 扩展 javascript 代码片段究竟是如何工作的?
How exactly does the following Chrome Extension javascript code snippet work?
我没有得到{urls: ["://www.mysite.com/.js"]},
["blocking"]);部分。由于 Whosebug 规则,将 mysite(dot)com 稍微(强制)修改为 example.com,可以在@Chrome extension to modify page's script includes and JS 找到此代码。
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if( details.url == "http://www.example.com/js/library.js" )
return {redirectUrl: "http://www.example.com/js/library_dev.js" };
},
{urls: ["*://www.example.com/*.js"]},
["blocking"]);
onBeforeRequest.addEventListener
的第二个参数是可选的requestFilter对象。
它有四个额外的属性
- urls(字符串的可选数组)
- 每个元素都是一个 URL 或 URL 模式。 URL 模式定义请参考内容脚本匹配模式。无法匹配任何 URL 的请求将被过滤掉。
- types(字符串的可选数组)
- 每个元素都是上述的请求类型。无法匹配任何类型的请求将被过滤掉。
- tabId(可选整数)
- 发生请求的选项卡的 ID。
- windowId(可选整数)
- 发生请求的 window 的 ID。
因此 urls: ["*://www.example.com/*.js"]}
正在向 onBeforeRequest 侦听器添加一个 URL 过滤器。
这是尝试匹配 www.example.com 域上对 javascript 文件的任何请求。使用 http 或 https
[(方案)*]:\\[(主机)www.example.com]/ [(路径) *.js]
<url-pattern> := <scheme>://<host><path>
<scheme> := '*' | 'http' | 'https' | 'file' | 'ftp'
<host> := '*' | '*.' <any char except '/' and '*'>+
<path> := '/' <any chars>
https://developer.chrome.com/extensions/match_patterns
第三个参数['blocking']
是一个可选的字符串数组,用于提供额外信息。这会修改 Chrome returns 您的回调方式。
由于包含"blocking",回调函数是同步处理的。这意味着请求被阻塞,直到回调函数returns。因此,对于 onBeforeRequest,您可以使用它在请求发生之前取消或重定向请求。
此处提供更多信息:
我没有得到{urls: ["://www.mysite.com/.js"]}, ["blocking"]);部分。由于 Whosebug 规则,将 mysite(dot)com 稍微(强制)修改为 example.com,可以在@Chrome extension to modify page's script includes and JS 找到此代码。
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if( details.url == "http://www.example.com/js/library.js" )
return {redirectUrl: "http://www.example.com/js/library_dev.js" };
},
{urls: ["*://www.example.com/*.js"]},
["blocking"]);
onBeforeRequest.addEventListener
的第二个参数是可选的requestFilter对象。
它有四个额外的属性
- urls(字符串的可选数组)
- 每个元素都是一个 URL 或 URL 模式。 URL 模式定义请参考内容脚本匹配模式。无法匹配任何 URL 的请求将被过滤掉。
- types(字符串的可选数组)
- 每个元素都是上述的请求类型。无法匹配任何类型的请求将被过滤掉。
- tabId(可选整数)
- 发生请求的选项卡的 ID。
- windowId(可选整数)
- 发生请求的 window 的 ID。
因此 urls: ["*://www.example.com/*.js"]}
正在向 onBeforeRequest 侦听器添加一个 URL 过滤器。
这是尝试匹配 www.example.com 域上对 javascript 文件的任何请求。使用 http 或 https
[(方案)*]:\\[(主机)www.example.com]/ [(路径) *.js]
<url-pattern> := <scheme>://<host><path>
<scheme> := '*' | 'http' | 'https' | 'file' | 'ftp'
<host> := '*' | '*.' <any char except '/' and '*'>+
<path> := '/' <any chars>
https://developer.chrome.com/extensions/match_patterns
第三个参数['blocking']
是一个可选的字符串数组,用于提供额外信息。这会修改 Chrome returns 您的回调方式。
由于包含"blocking",回调函数是同步处理的。这意味着请求被阻塞,直到回调函数returns。因此,对于 onBeforeRequest,您可以使用它在请求发生之前取消或重定向请求。
此处提供更多信息: