-1

我想將Facebook SDK和登錄按鈕注入網頁,但它返回了一個我不明白的錯誤。我正在使用Firefox附加組件生成器。 理想的解決方案是能夠注入一個HTML 文件而不是隻是代碼,但我不知道如何做到這一點。以下是main.js上的相關代碼。我知道這是正確的:將HTML插入網頁

var pageMod = require("page-mod"); 
pageMod.PageMod({ 
    include: "http://www.blogger.com/*", 
    contentScriptFile: [self.data.url("jquery-latest.js"), 
         self.data.url("blogger.js")] 
}); 

這裏是blogger.js

$('body').prepend(
    '<div id="fb-root"></div>' + 
    '<script>' + 
    ' window.fbAsyncInit = function() {' + 
    ' FB.init({' + 
    '  appId  : "XXXXXXXXXXXXXXX", // App ID' + 
    '  status  : true, // check login status' + 
    '  cookie  : true, // enable cookies to allow the server to access the session' + 
    '  xfbml  : true // parse XFBML' + 
    ' });' + 
    ' // Additional initialization code here' + 
    ' };' + 
    ' // Load the SDK Asynchronously' + 
    ' (function(d){' + 
    '  var js, id = "facebook-jssdk", ref = d.getElementsByTagName("script")[0];' + 
    '  if (d.getElementById(id)) {return;}' + 
    '  js = d.createElement("script"); js.id = id; js.async = true;' + 
    '  js.src = "//connect.facebook.net/en_US/all.js";' + 
    '  ref.parentNode.insertBefore(js, ref);' + 
    ' }(document));' 
    '$("button.blogg-button.blott-primary").click(function() {' + 
    ' FB.login(function(response) {' + 
    '  if (response.authResponse) {' + 
    '   console.log("Welcome! Fetching your information.... ");' + 
    '   FB.api("/me", function(response) {' + 
    '   console.log("Good to see you, " + response.name + ".");' + 
    '  });' + 
    '  } else {' + 
    '   console.log("User cancelled login or did not fully authorize.");' + 
    '  }' + 
    ' }, {scope: "publish_actions,manage_pages"});' + 
    '});' + 
    '</script>' 
    ) 

這裏是錯誤(從Firefox的錯誤控制檯):

Timestamp: 5/31/2012 8:53:28 AM 
Error: An exception occurred. 
Traceback (most recent call last): 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/addon-kit/lib/page-mod.js", line 195, in onRead 
    self._createWorker(window); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/addon-kit/lib/page-mod.js", line 203, in _createWorke 
    onError: this._onUncaughtError 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/traits.js", line 110, in Trai 
    return self.constructor.apply(self, arguments) || self._public; 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/content/worker.js", line 403, in Worke 
    this._contentWorker = WorkerSandbox(this); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/traits.js", line 110, in Trai 
    return self.constructor.apply(self, arguments) || self._public; 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/content/worker.js", line 202, in WorkerSandbo 
    this._importScripts.apply(this, contentScriptFile); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/content/worker.js", line 260, in _importScript 
    load(this._sandbox, String(uri)); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/sandbox.js", line 43, in loa 
    return scriptLoader.loadSubScript(uri, sandbox, 'UTF-8'); 
SyntaxError: missing) after argument list 

編輯:我已經忘了在行尾添加加號,但修復不能解決問題。但是,錯誤代碼已更改。

' }(document));' 
    ' }(document));' + 

新的錯誤代碼:

Timestamp: 5/31/2012 9:12:06 AM 
Error: An exception occurred. 
Traceback (most recent call last): 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/addon-kit/lib/page-mod.js", line 195, in onRead 
    self._createWorker(window); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/addon-kit/lib/page-mod.js", line 203, in _createWorke 
    onError: this._onUncaughtError 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/traits.js", line 110, in Trai 
    return self.constructor.apply(self, arguments) || self._public; 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/content/worker.js", line 403, in Worke 
    this._contentWorker = WorkerSandbox(this); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/traits.js", line 110, in Trai 
    return self.constructor.apply(self, arguments) || self._public; 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/content/worker.js", line 202, in WorkerSandbo 
    this._importScripts.apply(this, contentScriptFile); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/content/worker.js", line 260, in _importScript 
    load(this._sandbox, String(uri)); 
    File "resource://jid0-nsaawinaml0b6pn0st2hc4vinxi-at-jetpack/api-utils/lib/sandbox.js", line 43, in loa 
    return scriptLoader.loadSubScript(uri, sandbox, 'UTF-8'); 
SyntaxError: missing ; before statement 

回答

1

嘗試打破<script></script>'<sc'+'ript>''</sc'+'ript>'因此它不覺得劇本已經提前結束。

+0

這是一個JavaScript文件,而不是HTML頁面... –

+0

對於Firefox,使用'<\/script>'而不是''應該足夠了。 – user2428118

+0

@ user1419007:這隻會在HTML文件的'

0

好的,我修好了。我是愚蠢的,我在代碼中修正了錯字之後,不小心將一些文本粘貼到了文件中(請參閱編輯),並導致語法錯誤。