将HTML插入网页

时间:2012-05-31 13:09:42

标签: javascript jquery facebook-graph-api firefox-addon firefox-addon-sdk

我正在尝试将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

2 个答案:

答案 0 :(得分:1)

尝试将<script></script>分解为'<sc'+'ript>''</sc'+'ript>',这样就不会认为脚本过早结束了。

答案 1 :(得分:0)

好的,我修好了。我是愚蠢的,在修复代码中的拼写错误后,我不小心将一些文本粘贴到文件中(参见编辑),这导致语法错误。