Chrome扩展程序将标签重定向到我的页面 [英] Chrome extension to redirect tab to my page

查看:197
本文介绍了Chrome扩展程序将标签重定向到我的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对chrome扩展是新手,并且在为Chrome编写我的第一个扩展时遇到了麻烦。我试图做的是检测新的标签操作并重定向到预定义的(我的网页)url。



我写了manifest.json和背景。 HTML,但它似乎并没有工作。
我经历了关于函数的google论文以及如何使用它们,但肯定有一些我错过了。



我不太了解我在这里所做的一些事情,例如manifest.json中的内容脚本。



有些帮助解决这个问题,值得赞赏。 b
$ b

编辑





EDIT2
这里是更新的代码
现在我不'没有background.html文件。
manifest.json

  {
name:Tabber,
manifest_version :2,
version:1.0,
description:MyExtension,
background:{
scripts:[code.js ]
},
chrome_url_overrides:{
newtab:code.js
},
permissions:[
标签

}

code.js

 < script> 
chrome.browserAction.onClicked.addListener(function(tab){
var action_url =www.xyz.com
chrome.tabs.create({url:action_url});

$ lt; / script>

现在当我打开一个新选项卡时,查看显示的.js文件的来源
截图:



为什么不执行脚本?

解决方案

想要在默认页面上运行,您应该使用html文件,而不是js文件。
如果您只是想骑一个页面,则不需要任何背景页面或内容脚本。是示例代码:

menifest.json:

  {
name:Tabber,
manifest_version:2,
version:1.0,
description:MyExtension,
chrome_url_overrides:{
newtab:my.html
},
permissions:[
标签
]
}

my.html:

 <!DOCTYPE html> 
< html>
< head>
< title>翻页< / title>
< script type =text / javascriptsrc =code.js>
< / script>
< / head>
< body>
< / body>
< / html>

code.js:

  window.open(http://www.google.com,_self); 

注意:您不能在html文件中写入任何js。你需要在html文件中包含js文件。



而且,你甚至不需要任何js文件。只需修改my.html为:

 < head> 
< meta http-equiv =refreshcontent =0; URL = http://www.google.com>
< / head>


I am new to chrome extensions and I am having trouble while writing my first extension for chrome. What I am trying to do is detect the new tab action and redirect to a pre-defined(my webpage) url.

I wrote the manifest.json and the background.html but it doesn't seem to work. I went through the google papers on the functions and how to use them, but there must be something I've missed.

I do not understand quite a few things that I've done in here, for example the content-scripts in manifest.json.

Some help in fixing this would be appreciated.

EDIT

EDIT2 Here is the updated code Now I don't have the background.html file. manifest.json

{
  "name": "Tabber",
  "manifest_version" : 2,
  "version": "1.0",
  "description": "MyExtension",
  "background" : {
    "scripts": ["code.js"]
  },
  "chrome_url_overrides": {
      "newtab": "code.js"
   },
  "permissions": [
    "tabs"
  ]
}

code.js

<script>
    chrome.browserAction.onClicked.addListener(function(tab){
            var action_url = "www.xyz.com"
            chrome.tabs.create({ url: action_url });
    }
</script>

Now when I open a new tab, I get to see the source of the .js file displayed. Screenshot:

Why isn't the script being executed ?

解决方案

If you want to over ride a default page, you shoud use html file, not a js file. And if you just want to over ride a page, you do not need any background page or content script. Here is the sample code:

menifest.json:

{
    "name": "Tabber",
    "manifest_version" : 2,
    "version": "1.0",
    "description": "MyExtension",
    "chrome_url_overrides": {
        "newtab": "my.html"
    },
    "permissions": [
        "tabs"
    ]
}

my.html:

<!DOCTYPE html>
<html>
<head>
    <title>over ride page</title>   
    <script type="text/javascript" src="code.js">
    </script>
</head>
<body>    
</body>
</html>

code.js:

window.open("http://www.google.com", "_self");

Note: you can not write any js in your html file. You need to include js file in html file.

And, you even do not need any js file. just modify the my.html as this:

<head>
    <meta http-equiv="refresh"content="0;URL=http://www.google.com">
</head>

这篇关于Chrome扩展程序将标签重定向到我的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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