将当前网页的网址显示为Chrome扩展程序 [英] Display the url of current web page into a chrome extension

查看:124
本文介绍了将当前网页的网址显示为Chrome扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Chrome扩展程序中显示当前网页的网址。我刚刚开始使用JavaScript和Chrome扩展开发。



我的manifest.json

  {
manifest_version:2,
version:0.1,
name:!!!,
author:! !,
description:Extension !!!,
图标:{
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 Extension !!!!!!!!!< / title>
< link rel =stylesheethref =stylesheet.css>
<! - < script type =text / javascriptsrc =tracker.js> < /脚本>
< 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>
<! - =============显示网址=========== - >
< div id =main-url>
< script src =background.js>< / script>
< / div>
< / div>




我的内容.js

  chrome.extension.sendRequest({
url:window.location.href
},函数(响应){
console.log(response.farewell);
);

我的background.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(
函数(request,sender,sendResponse){
if(request.url ===http:// xxxxx .com)){
chrome.runtime.sendMessage({farewell:bingo},function(response){});
}
}
);

顺便说一句,正如@Teepeemm所说的,您不必加载内容脚本和背景js两次。 contentscript.js将根据匹配模式注入到web上下文中。当扩展开始运行时,background.js将被执行。

I try to display the URL of the current web page in a chrome extension. I just begin javascript and chrome extension dev.

My manifest.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://*/*"]
}

My 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>

My contentscript.js

chrome.extension.sendRequest({
url: window.location.href
}, function(response) {
console.log(response.farewell);
);

My background.js

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
    console.log(sender.tab ?
        "from a content script:" + sender.tab.url :
        "from the extension !!TEST!!");
}
);

Nothing diplay in my popup and in the console...

Thanks for your help Best regards

解决方案

chrome.extension.sendRequest is deprecated. You should use chrome.runtime.sendMessage:

Sender

chrome.runtime.sendMessage({
  url: window.location.href
}, function(response) {
  console.log(response.farewell);
);

Receiver

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if(request.url==="http://xxxxx.com"){
        chrome.runtime.sendMessage({farewell:"bingo"},function(response){});
    }
  }
);

BTW, as @Teepeemm said, you don't have to load content script and background js twice. contentscript.js will be injected into web context according to the matches pattern. And background.js will be executed once the extension start running

这篇关于将当前网页的网址显示为Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆