2012-11-30 15 views
10

這裏是我的腳本,它使用onUpdated.addListener檢查每個網址:chrome.tabs.onUpdated.addListener越來越完整的狀態兩次或兩次以上的每一頁

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> 

在此先感謝您的時間。

+1

你想停止髮型? onUpdated事件或? – Sudarshan

+0

它不能爲每個iframe觸發,'chrome.tabs.onUpdated'用於製表符而不是頁面。所以每個標籤只會出現一次。 – ocanal

+0

當打開一個新選項卡時,onUpdated偵聽程序將完成兩次狀態更改。條件if(changeInfo.status =='complete')將根據頁面執行兩次或更多次。例如,當我打開Goog​​le主頁時,它會獲得兩次完整狀態。我的問題是如何多次停止更改完整狀態或僅在該狀態下執行腳本一次。 –

回答

7

這是Chrome Issue 162543中的一個已知錯誤,在2012年12月5日標記爲已修復。

解決方法:(不再需要)

Make Event Page to Background Page通過改變代碼

"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()根本不會在content scripts中工作,請刪除此代碼

讓我知道您是否需要更多信息。

+0

非常感謝您的支持和寶貴的意見。我實現了'解決方案一',它工作正常,也感謝指出我在內容腳本中使用addlistener()的錯誤。 –

+0

這似乎在OS X上的28.0.1500.71上再次被打破,只是要小心。 –

+0

我也看到兩個「完整」類型的事件,但它們稍有不同。第二個參數changeInfo對於第一個事件是「完整的」,對於第二個參數是未定義的。我在https://code.google.com/p/chromium/issues/detail?id=162543上提供了我的觀察結果 – kzahel

相關問題