我正在尝试将 Facebook SDK 和登录按钮注入网页,但它返回了一个我不明白的错误。我正在使用 Firefox Add-on Builder。理想的解决方案是能够注入 HTML文件而不仅仅是代码,但我不知道该怎么做。下面是相关代码main.js
。我知道这是正确的:
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