我一直在关注教程,但没有人会使用扩展程序打开链接!我尝试了这个代码,除了显示加载标志并且消失之外它什么都不做。 的manifest.json:
{
"name": "popphelp Helpful Links",
"version": "0.1",
"description": "Get to helpful links faster!",
"manifest_version": 2,
"permissions": [ "tabs", "storage", "http://*/*", "https://*/*" ],
"background": {
"scripts": [ "jquery-1.11.3.min.js", "background.js" ],
"persistent": false
},
"browser_action": {
"defualt_icon": "icon.png",
"default_popup": "mypopup.html"
}
}
background.js:
// background.js
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tabs) {
// Send a message to the active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
});
});
// This block is new!
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "open_new_tab" ) {
chrome.tabs.create({"url": request.url});
}
}
);
content.js:
// content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "clicked_browser_action" ) {
var firstHref = $("a[href^='http']").eq(0).attr("href");
console.log(firstHref);
// This line is new!
chrome.runtime.sendMessage({"message": "open_new_tab", "www.google.com": firstHref});
}
}
);
jquery的:
<script
src="http://code.jquery.com/ui/1.11.43/jquery-ui.js"
integrity="sha256-DI6NdAhhFRnO2k51mumYeDShet3I8AKCQf/tf7ARNhI="
crossorigin="anonymous"></script>