试图让mathjax在facebook上加载以呈现评论

时间:2015-10-17 02:25:50

标签: javascript facebook developer-tools

我有以下脚本适用于大多数博客网站(在开发人员工具的控制台中输入时)但是facebook:



(function() {


  /* insert the MathJax script dynamically into the document */
  /* also insert a fix for Google+, until fixed upstream in MathJax */
  function insertScript(doc) {

    var googleFix = '.MathJax .mn {background: inherit;} .MathJax .mi {color: inherit;} .MathJax .mo {background: inherit;}';
    var style = doc.createElement('style');
    style.innerText = googleFix;
    try {
      style.textContent = googleFix;
    } catch (e) {}
    doc.getElementsByTagName('body')[0].appendChild(style);

    var script = doc.createElement('script'),
      config;

    /* see http://www.mathjax.org/resources/faqs/#problem-https */
    script.src = '//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js';
    script.type = 'text/javascript';

    /* see http://docs.mathjax.org/en/v1.1-latest/options/tex2jax.html */
    config = 'MathJax.Ajax.config.path[\'Contrib\']=\'//cdn.mathjax.org/mathjax/contrib\';MathJax.Hub.Config({tex2jax:{inlineMath:[[\'$\',\'$\'],[\'$$\', \'$$\']],displayMath:[[\'\\\\[\',\'\\\\]\']],processEscapes:true},TeX:{extensions: [\'[Contrib]/xyjax/xypic.js\']}});MathJax.Hub.Startup.onload();';

    if (window.opera) script.innerHTML = config;
    else script.text = config;

    doc.getElementsByTagName('head')[0].appendChild(script);
  }

  /* execute MathJax for given window */
  function executeMathJax(win) {
    if (win.MathJax === undefined) {
      /* insert the script into document if MathJax global doesn't exist for given window */
      insertScript(win.document);
    } else {
      /* using win.Array instead of [] to get 'instanceof Array' check working inside iframe */
      /* see http://www.mathjax.org/docs/1.1/typeset.html */
      win.MathJax.Hub.Queue(new win.Array('Typeset', win.MathJax.Hub));
    }
  }

  var frames = document.getElementsByTagName('iframe'),
    index, win;

  /* execute MathJax on the window object */
  executeMathJax(window);

  /* try to execute MathJax on every iframe */
  for (index = 0; index < frames.length; index++) {
    /* find the iframe's window object */
    win = frames[index].contentWindow || frames[index].contentDocument;
    if (!win.document) win = win.parentNode;

    executeMathJax(win);
  }
})();
&#13;
&#13;
&#13;

在facebook上加载时,我收到以下错误: Refused to load the script 'https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js' because it violates the following Content Security Policy directive: "script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl". 我真的无法弄清楚这是什么或如何规避它。我尝试在Facebook上发布错误报告,但不确定何时会回复我,我还尝试通过在控制台中进行copypasting来加载整个mathjax.js,但后来我不知道如何更改我的代码片段。

1 个答案:

答案 0 :(得分:-2)

(function() {


  /* insert the MathJax script dynamically into the document */
  /* also insert a fix for Google+, until fixed upstream in MathJax */
  function insertScript(doc) {

    var googleFix = '.MathJax .mn {background: inherit;} .MathJax .mi {color: inherit;} .MathJax .mo {background: inherit;}';
    var style = doc.createElement('style');
    style.innerText = googleFix;
    try {
      style.textContent = googleFix;
    } catch (e) {}
    doc.getElementsByTagName('body')[0].appendChild(style);

    var script = doc.createElement('script'),
      config;

    /* see http://www.mathjax.org/resources/faqs/#problem-https */
    script.src = '//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js';
    script.type = 'text/javascript';

    /* see http://docs.mathjax.org/en/v1.1-latest/options/tex2jax.html */
    config = 'MathJax.Ajax.config.path[\'Contrib\']=\'//cdn.mathjax.org/mathjax/contrib\';MathJax.Hub.Config({tex2jax:{inlineMath:[[\'$\',\'$\'],[\'$$\', \'$$\']],displayMath:[[\'\\\\[\',\'\\\\]\']],processEscapes:true},TeX:{extensions: [\'[Contrib]/xyjax/xypic.js\']}});MathJax.Hub.Startup.onload();';

    if (window.opera) script.innerHTML = config;
    else script.text = config;

    doc.getElementsByTagName('head')[0].appendChild(script);
  }

  /* execute MathJax for given window */
  function executeMathJax(win) {
    if (win.MathJax === undefined) {
      /* insert the script into document if MathJax global doesn't exist for given window */
      insertScript(win.document);
    } else {
      /* using win.Array instead of [] to get 'instanceof Array' check working inside iframe */
      /* see http://www.mathjax.org/docs/1.1/typeset.html */
      win.MathJax.Hub.Queue(new win.Array('Typeset', win.MathJax.Hub));
    }
  }

  var frames = document.getElementsByTagName('iframe'),
    index, win;

  /* execute MathJax on the window object */
  executeMathJax(window);

  /* try to execute MathJax on every iframe */
  for (index = 0; index < frames.length; index++) {
    /* find the iframe's window object */
    win = frames[index].contentWindow || frames[index].contentDocument;
    if (!win.document) win = win.parentNode;

    executeMathJax(win);
  }
})();