chrome.tabs.onUpdated.addListener为每个页面获得两次或更多的完整状态 [英] chrome.tabs.onUpdated.addListener is getting complete status twice or more for each page

查看:240
本文介绍了chrome.tabs.onUpdated.addListener为每个页面获得两次或更多的完整状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的脚本,它使用onUpdated.addListener检查每个url:

  chrome.tabs.onUpdated.addListener(函数(tabId,changeInfo,tab){
alert(changeInfo.status);
if(changeInfo.status =='complete'){
if(tab.url.indexOf(in .yahoo.mail)!== -1){
alert(tab.url);
chrome.tabs.update(tabId,{url:https://accounts.google.com/ ServiceLogin});
// injectToTab(tab);
}
}
});

这里是Manifest代码:

 {
name:LeoPlugin For Test,
version:1.6,
manifest_version:2,
content_security_policy:script-src'self'; object-src'self',
description:自动扩展名,
background:{
脚本:[js / eventPage.js],
persistent:false
},
content_scripts:[
{
matches: [http:// * / *],
js:[js / eventPage.js]
}
],
图标:{ 48:images / bob48.png,128:images / bob128.png},//定义任何图标大小以及您想要使用的文件。 48/128等
browser_action:{
default_icon:images / bob.png,//你想在chrome工具栏上显示什么图标
default_popup :testwatch.html//点击按钮时弹出的页面。
},
permissions:[
tabs,http:// * / *,https:// * / *//跨站点访问请求


$ b $ / code>

上述代码中缺少的任何内容可能会阻止不止一次地获得完整状态。这里是我的HTML代码:

 < html> 
< head>
< title> Leo Chrome扩展程序< / title>

< link href =css / main.css =stylesheettype =text / css/>
< link href =css / fonts.css =stylesheettype =text / css/>
< script type =text / javascriptsrc =jquery / jquery-1.4.2.min.js>< / script>
< script type =text / javascriptsrc =jquery / jquery-ui-1.8.2.custom.min.js>< / script>
<! - < script type =text / javascriptsrc =js / Pluginjshelper.js>< / script> - >
< / head>
< body class =inner_bg>
< div class =gray-panel>
< div>
< div class =user-pic>
< img src =Images / leo-logo.pngwidth =70height =50/>
< / div>
< h3>
Leo Extension< / h3>< input name =oktype =buttonclass =btnid =button123value =OK/> <! - onclick =getCPRepGraph() - >
< input name =chkEnableid =chkPluginEnabletype =checkboxvalue =class =check/>< span class =lbl>启用< / span>
< / div>
< div class =clear>
< / div>
< div>
< h4>
< span class =blue>插件< / span>凭证< / h4>
< ul class =addpage-form2>
< li>
< label class =addpage-label>
用户名:< / label>< input name =txtUserNameid =txtUserNametype =textclass =addpage-input2/>
< / li>
< li>
< label class =addpage-label>
密码:< / label>< input name =txtPasswordid =txtPasswordtype =passwordclass =addpage-input2/>
< / li>
< / ul>
< div class =clear>
< / div>
< / div>
<! - BASIC INFO面板结束 - >
< div class =clear>
< / div>
< / div>
< / body>
< / html>

预先感谢您的时间。

解决方案

这是Chrome Issue 162543 ,它在2012年12月5日标记为已修复。



解决方法: strong>(不再需要)



将活动页面设为后台页面通过更改

 background中的代码:{
scripts:[js /eventPage.js],
persistent:false
},

 background:{
scripts:[js / eventPage.js],
persistent:true
},

并使用后台页面开发直到修复交付。



其他要点:

看看你的manifest.json为什么

 background:{
scripts:[js / eventPage.js],
persistent:false
},
content_scripts:[
{
:[http:// * / *],
js:[js / eventPage.js]
}
],

js / eventPage.js 是内容脚本以及background \event脚本;获取 chrome.tabs.onUpdated.addListener()的代码将无法在内容脚本,请删除此代码



让我知道如果你需要更多信息。


Here is my Script, which uses onUpdated.addListener to check each url:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {          
 alert(changeInfo.status);
 if (changeInfo.status == 'complete') {
 if (tab.url.indexOf("in.yahoo.mail") !== -1) {
 alert(tab.url);
 chrome.tabs.update(tabId, { url: "https://accounts.google.com/ServiceLogin" });
  //injectToTab(tab);
  }
 }
}); 

And here is the Manifest code:

 {
  "name": "LeoPlugin For Test",
  "version": "1.6",
  "manifest_version": 2,
  "content_security_policy": "script-src 'self'; object-src 'self'",
  "description": "Extension to Automate.",
  "background": {
    "scripts": ["js/eventPage.js"],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["js/eventPage.js"]
    }
  ],
  "icons":{"48":"images/bob48.png", "128":"images/bob128.png"}, //Define any icon sizes and the files that you want to use with them. 48/128 etc.
  "browser_action": {
    "default_icon": "images/bob.png",       // What icon do you want to display on the chrome toolbar
    "default_popup": "testwatch.html"       // The page to popup when button clicked.
  },
  "permissions": [
    "tabs", "http://*/*","https://*/*"             // Cross Site Access Requests
  ]

}

Anything missing in the above code that can prevent getting 'complete' status more than once. And also here is my HTML code:

<html>
  <head>
    <title>Leo Chrome Extension</title>

    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <link href="css/fonts.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="jquery/jquery-ui-1.8.2.custom.min.js"></script>
    <!--<script type="text/javascript" src="js/Pluginjshelper.js"></script>-->
</head>
<body class="inner_bg">
        <div class="grey-panel">
            <div>
            <div class="user-pic">
                <img src="Images/leo-logo.png" width="70" height="50" />
            </div>
            <h3>
                Leo Extension</h3><input name="ok" type="button" class="btn" id = "button123"  value="OK"/> <!--onclick = "getCPRepGraph()" -->
                <input name="chkEnable" id="chkPluginEnable" type="checkbox" value="" class="check"  /><span class="lbl">Enable</span>
            </div>
            <div class="clear">
            </div>
            <div>
                <h4>
                    <span class="blue">Plugin </span>Credentials</h4>
                <ul class="addpage-form2">
                    <li>
                        <label class="addpage-label">
                            User Name :</label><input name="txtUserName" id="txtUserName" type="text" class="addpage-input2" />
                    </li>
                    <li>
                        <label class="addpage-label">
                            Password :</label><input name="txtPassword" id="txtPassword" type="password" class="addpage-input2" />
                    </li>
                </ul>
                <div class="clear">
                </div>
            </div>
            <!--BASIC INFO panel End-->
            <div class="clear">
            </div>
        </div>
</body>
</html>

Thanks in advance for your time.

解决方案

This was a known bug in Chrome Issue 162543 which was marked as fixed on 2012-12-05.

Workaround: (not needed anymore)

Make Event Page to Background Page by changing code from

"background": {
    "scripts": ["js/eventPage.js"],
    "persistent": false
  },

to

"background": {
    "scripts": ["js/eventPage.js"],
    "persistent": true
  },

and moving ahead by using background pages for development till the fix is delivered.

Other Points:

After a look at your manifest.json why does

"background": {
    "scripts": ["js/eventPage.js"],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["js/eventPage.js"]
    }
  ],

js/eventPage.js is made content script as well as background\event script; Having code for chrome.tabs.onUpdated.addListener() will not work in content scripts at all, please eliminate this code

Let me know if you need more information.

这篇关于chrome.tabs.onUpdated.addListener为每个页面获得两次或更多的完整状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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