将当前网页的url显示为chrome扩展
Display the url of current web page into a chrome extension
我尝试在chrome扩展中显示当前网页的URL。我刚开始javascript和chrome扩展dev.
我的宣言.json
{
"manifest_version": 2,
"version": "0.1",
"name": "!!!",
"author": "!!!",
"description": "Extension !!!",
"icons": {
"default_icon": "logo2.png"
},
"browser_action": {
"default_icon": "logo2.png",
"default_popup": "popup.html"
},
"options_page": "options.html",
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": ["contentscript.js"]
}],
"web_accessible_resources": ["contentscript.js"],
"permissions": ["tabs", "http://*/*", "https://*/*"]
}
我的popup.html:
<!doctype html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Extension !!!!!!!!!</title>
<link rel="stylesheet" href="stylesheet.css">
<!--<script type="text/javascript" src="tracker.js"> </script>
<script src="canvas.js"></script>-->
<script src="background.js"></script>
<script src="contentscript.js"></script>
</head>
<body>
<!--=========TITLE============== -->
<div id="main-logo">
<div id="logo"><img src="logo2.png"><img></div>
<h1>!!!!</h1>
</div>
<div id="main">
<!--=============Display URL===========-->
<div id="main-url">
<script src="background.js"></script>
</div>
</div>
我的contentscript.js
chrome.extension.sendRequest({
url: window.location.href
}, function(response) {
console.log(response.farewell);
);
我的背景.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension !!TEST!!");
}
);
我的弹出窗口和控制台中没有显示任何内容。。。
谢谢你的帮助向致以最良好的问候
chrome.extension.sendRequest已弃用。您应该使用chrome.runtime.sendMessage:
发件人
chrome.runtime.sendMessage({
url: window.location.href
}, function(response) {
console.log(response.farewell);
);
接收器
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.url==="http://xxxxx.com"){
chrome.runtime.sendMessage({farewell:"bingo"},function(response){});
}
}
);
顺便说一句,正如@Teepeemm所说,你不必两次加载内容脚本和背景js。contentscript.js将根据匹配模式注入到web上下文中。一旦扩展开始运行
相关文章:
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 如何创建一个“;表单弹出框“;在chrome中右键单击时位于突出显示的单词上方
- 在firefox和chrome中的左侧显示iframe滚动条
- Chrome(webkit?)无法在幻灯片中正确显示图像
- Firefox赢得了'不会显示我添加的jquery元素,但Chrome会显示
- 无法在Chrome(打包)应用程序上检测到ESC按钮(全屏显示时)
- Chrome和Firefox之间的Wordpress显示差异
- 向下滚动时隐藏菜单,向上滚动时显示,适用于Chrome,不适用于Safari(手机)
- Chrome 开发工具:如何计算加载弹出窗口并将其显示在页面上所需的总时间
- 在Chrome扩展程序中,是否可以截屏并在弹出窗口中显示屏幕
- 一系列下拉菜单,由jQuery提供支持,隐藏和显示,但不在Chrome中
- 显示chrome.storage中的所有内容
- 仅当标签页处于非活动状态时,才会显示 Chrome 桌面通知
- 未显示 Chrome 扩展程序浏览器操作
- 如何请求在popup.html中显示chrome扩展的清单版本号
- 如何选择性地显示Chrome扩展'的工具栏图标
- 超链接图像不显示Chrome扩展
- Ubermenu子菜单不显示Chrome/Safari
- Javascript:总是显示Chrome地址栏
- history.back()显示Chrome的问题