我想写一个微不足道的Chrome页面扩展,将一个页面上的所有锚点从一个域更改为另一个域......但我似乎无法完成它的工作,在调试时遇到麻烦。微小的Chrome页面扩展不起作用
我是否误解了这种扩展需要如何构建?或者我只是滥用API?
manifest.json的:
{
"name": "theirs2ours",
"version": "1.0",
"description": "Changes all 'their' URLs to 'our' URLs.",
"background_page": "background.html",
"permissions": [
"tabs"
],
"page_action": {
"default_icon": "cookie.png",
"default_title": "theirs2ours"
},
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["content.js"]
}
]
}
background.html:
<html>
<head>
<script type='text/javascript'>
chrome.tabs.onSelectionChanged.addListener(function(tabId) {
chrome.pageAction.show(tabId);
});
chrome.tabs.getSelected(null, function(tab) {
chrome.pageAction.show(tab.id);
});
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.sendRequest(tab.id, {}, null);
});
</script>
</head>
<body>
</body>
</html>
content.js:
var transform = function() {
var theirs = 'http://www.yourdomain.com';
var ours = 'http://sf.ourdomain.com';
var anchors = document.getElementsByTagName('a');
for (var a in anchors) {
var link = anchors[a];
var href = link.href;
if (href.indexOf('/') == 0) link.href = ours + href;
else if (href.indexOf(theirs) == 0) link.href = href.replace(theirs, ours);
}
};
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
transform();
});
注证明的API已经通过名称和签名变了 - 今天有同样的效果,你需要['chrome.tabs.onActivated'(http://developer.chrome.com/extensions/tabs。 html#event-onActivated)和['chrome.tabs.getCurrent'](http://developer.chrome.com/extensions/tabs.html#method-getCurrent)。 – Ronny 2013-06-13 11:01:52