0

我試圖爲我的Google App Engine(Python)服務器創建一個渠道,但似乎存在問題,但我不確定原因。當用戶切換擴展名時,它會對用戶進行身份驗證。如果成功,服務器會回覆一個我用來創建頻道的頻道令牌。當我對用戶進行身份驗證時,出現alert("a"),但是alert("b")不會讓我相信var channel = new goog.appengine.Channel(msg.token);行有問題,但控制檯不報告錯誤。Chrome擴展程序 - 渠道無法正常工作

我也複製了來自here的javascript代碼,並將其放在我的清單中,反對將<script type="text/javascript" src="/_ah/channel/jsapi"></script>放在background.html中。

//script.js 
function authenticate(callback) { 
    var url = "https://r-notes.appspot.com/init/api/authenticate.json?username=" + username + "&password=" + password; 
    $.post(url, function(data) { 
     if (data.status == "200") { 
      channelToken = data.channeltoken; 
      if (callback) { 
       callback(); 
      } 
      var port = chrome.extension.connect({name: "myChannel"}); 
      port.postMessage({token: channelToken}); 
      port.onMessage.addListener(function(msg) { 
       console.log(msg.question); 
      });   
     } 
    }); 
} 


//background.html 
chrome.extension.onConnect.addListener(function(port) { 
    port.onMessage.addListener(function(msg) { 
     alert("a"); //pops up 
     var channel = new goog.appengine.Channel(msg.token); 
     alert("b"); //does not pop up 
     console.log(channel); //display error ' Error in event handler for 'undefined': ReferenceError: goog is not defined ' 
     var socket = channel.open() 
     socket.onopen = function() { 
      // Do stuff right after opening a channel 
      console.log('socket opened'); 
     } 
     socket.onmessage = function(evt) { 
      // Do more cool stuff when a channel message comes in 
      console.log('message recieved'); 
      console.log(evt); 
     } 
    }); 
}); 


//manifest.json 
{ 
    "name": "moot", 
    "description": "Clicking on the moot button will display a sidebar!", 
    "version": "0.2.69", 
    "background_page": "html/background.html", 
    "browser_action": { 
     "default_icon": "img/icon_64.png", 
     "default_title": "moot" 
    }, 
    "content_scripts": [ 
     { 
      "matches": ["<all_urls>"], 
      "js": ["js/channelApi.js", 
        "js/script.js", "js/mootsOnSidebar.js", "js/mootsOnPage.js", "js/authenticate.js", "js/otherFunctions.js", 
        "js/jquery/jquery-1.7.1.js", "js/jquery/jquery.mCustomScrollbar.js", "js/jquery/jquery-ui.min.js", 
        "js/jquery/jquery.autosize.js", "js/jquery/jquery.mousewheel.min.js", "js/jquery/jquery.easing.1.3.js", 
        "js/channel.js"], 
      "css": ["css/cssReset.css", "css/sidebar.css", "css/onPageCreate.css", "css/onPageExists.css", "css/scrollbar.css", "css/authenticate.css"] 
     } 
    ], 
    "permissions": [ 
     "tabs", "contextMenus", "http://*/*", "https://*/" 
    ], 
    "icons": { 
     "16": "img/icon_16.png", 
     "64": "img/icon_64.png" 
    } 
} 

編輯 - 做console.log(channel)後,我發現了錯誤「的事件處理程序錯誤爲‘未定義’:的ReferenceError:goog沒有定義」。我不確定爲什麼我收到這個錯誤,因爲我確實包含了所需的JavaScript文件,因爲我遵循了這個post

+1

你能顯示你的清單文件嗎?你有沒有要求正確的權限?通過'chrome:// extensions /'> Developer模式打開背景頁面(一個開發工具實例),並檢查是否有錯誤(同樣,在此Dev中,背景頁面內的任何'console.log'調用都將可見工具頁面)。 – 2012-04-03 14:36:47

+1

甜蜜絕招!我不知道你可以檢查console.log的背景頁面。 – Jon 2012-04-03 14:55:06

回答

1

因此,解決方法是您需要將文件<script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script>包含在HTML頁面中。我把它放在background.html的第一行。

我的錯誤是保存了channel.js的本地副本,並在manifest.json中引用它。

我現在要在我的服務器上放置一個channel.js的副本,並引用我的服務器副本。我不認爲會有任何問題。

0

在alert(「a」)和var channel = ... 之間爲msg direct的值創建一個控制檯日誌並檢查該值。

+0

我剛剛在'alert(「a」)'後面加了'console.log(msg)',但控制檯沒有報告任何東西。我回到了authenticate.js並重複檢查了console.log(channelToken)確實存在。我從Google的API中複製了消息傳遞代碼。 – Jon 2012-04-03 06:54:16

+0

等一下。我剛剛意識到......如果你在background.html中設置了console.log(),那麼這個回覆會在控制檯中顯示出來嗎?根據我的理解,這不應該是我下意識地使用'alerts()'的原因。 – Jon 2012-04-03 07:21:58

+0

我反而做了'alert(msg.token)',我確實收到了我的令牌的提示。 – Jon 2012-04-03 07:25:24

相關問題