我可以允许扩展用户选择匹配的域吗? [英] Can I allow the extension user to choose matching domains?
问题描述
我可以允许用户配置我的扩展的域匹配吗?我想让我的用户选择扩展程序的运行时间.
Can I allow the domain matching for my extension to be user configurable? I'd like to let my users choose when the extension runs.
推荐答案
要为内容脚本实现可定制的匹配模式",内容脚本需要在后台页面中使用 chrome.tabs.executeScript
方法(使用 chrome.tabs.onUpdated
事件监听器.
To implement customizable "match patterns" for content scripts, the Content script need to be executed in by the background page using the chrome.tabs.executeScript
method (after detecting a page load using the chrome.tabs.onUpdated
event listener).
因为匹配模式检查未在任何 API 中公开,您必须自己创建该方法.它在 url_pattern.cc 中实现
,该规范可在匹配模式处获得.
Because the match pattern check is not exposed in any API, you have to create the method yourself. It is implemented in url_pattern.cc
, and the specification is available at match patterns.
这是一个解析器的例子:
Here's an example of a parser:
/**
* @param String input A match pattern
* @returns null if input is invalid
* @returns String to be passed to the RegExp constructor */
function parse_match_pattern(input) {
if (typeof input !== 'string') return null;
var match_pattern = '(?:^'
, regEscape = function(s) {return s.replace(/[[^$.|?*+(){}\]/g, '\$&');}
, result = /^(*|https?|file|ftp|chrome-extension):///.exec(input);
// Parse scheme
if (!result) return null;
input = input.substr(result[0].length);
match_pattern += result[1] === '*' ? 'https?://' : result[1] + '://';
// Parse host if scheme is not `file`
if (result[1] !== 'file') {
if (!(result = /^(?:*|(*.)?([^/*]+))(?=/)/.exec(input))) return null;
input = input.substr(result[0].length);
if (result[0] === '*') { // host is '*'
match_pattern += '[^/]+';
} else {
if (result[1]) { // Subdomain wildcard exists
match_pattern += '(?:[^/]+\.)?';
}
// Append host (escape special regex characters)
match_pattern += regEscape(result[2]);
}
}
// Add remainder (path)
match_pattern += input.split('*').map(regEscape).join('.*');
match_pattern += '$)';
return match_pattern;
}
示例:在匹配模式的页面上运行内容脚本
在下面的示例中,数组是硬编码的.在实践中,您可以使用 localStorage
或 将匹配模式存储在数组中href="https://developer.chrome.com/extensions/storage.html">chrome.storage
.
// Example: Parse a list of match patterns:
var patterns = ['*://*/*', '*exampleofinvalid*', 'file://*'];
// Parse list and filter(exclude) invalid match patterns
var parsed = patterns.map(parse_match_pattern)
.filter(function(pattern){return pattern !== null});
// Create pattern for validation:
var pattern = new RegExp(parsed.join('|'));
// Example of filtering:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status === 'complete') {
var url = tab.url.split('#')[0]; // Exclude URL fragments
if (pattern.test(url)) {
chrome.tabs.executeScript(tabId, {
file: 'contentscript.js'
// or: code: '<JavaScript code here>'
// Other valid options: allFrames, runAt
});
}
}
});
要使其正常工作,您需要在清单文件中请求以下权限:
To get this to work, you need to request the following permissions in the manifest file:
"tabs"
- 启用必要的tabs
API."<all_urls>"
- 能够使用chrome.tabs.executeScript
在特定页面中执行内容脚本.
"tabs"
- To enable the necessarytabs
API."<all_urls>"
- To be able to usechrome.tabs.executeScript
to execute a content script in a specific page.
如果匹配模式集是固定的(即用户不能定义新模式,只能切换模式),"
可以替换为这组权限.您甚至可以使用可选权限来减少请求权限的初始数量(在chrome.permissions
的文档.
If the set of match patterns is fixed (ie. the user cannot define new ones, only toggle patterns), "<all_urls>"
can be replaced with this set of permissions. You may even use optional permissions to reduce the initial number of requested permissions (clearly explained in the documentation of chrome.permissions
).
这篇关于我可以允许扩展用户选择匹配的域吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!