chrome.tabs.onUpdated.addListener 为每个页面获取两次或更多次完整状态

2024-04-05

这是我的脚本,它使用 onUpdated.addListener 来检查每个 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);
  }
 }
}); 

这是清单代码:

 {
  "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
  ]

}

上述代码中缺少的任何内容都可能阻止多次获得“完成”状态。这是我的 HTML 代码:

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

在此先感谢您的时间。


这是 Chrome 中的一个已知错误问题 162543 https://code.google.com/p/chromium/issues/detail?id=162543已于 2012 年 12 月 5 日标记为已修复。

解决方法:(不再需要)

将事件页面设为背景页面 https://developer.chrome.com/extensions/event_pages.html#manifest通过更改代码

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

to

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

并通过使用后台页面进行开发继续前进,直到交付修复为止。

其他要点:

看了你的manifest.json后为什么

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

js/eventPage.js制作内容脚本以及背景\事件脚本;有代码chrome.tabs.onUpdated.addListener() https://developer.chrome.com/extensions/tabs.html#event-onUpdated不会工作在内容脚本 https://developer.chrome.com/extensions/content_scripts.html无论如何,请删除此代码

如果您需要更多信息,请与我们联系。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

chrome.tabs.onUpdated.addListener 为每个页面获取两次或更多次完整状态 的相关文章

随机推荐