2015-10-17 58 views
1

我有以下脚本(在控制台的开发工具进入时)在大多数博客网站作品,但Facebook的:试图让mathjax在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); 
 
    } 
 
})();

当在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上发布一个错误报告,但不知道他们什么时候会回复我,我也试图通过控制台中的副本载入整个mathjax.js,但后来我不知道如何更改我的代码段来处理这个问题。

+0

https://en.wikipedia.org/wiki/Content_Security_Policy - Facebook的简单指示浏览器不会允许系统从消息中提到的那些以外的任何域嵌入脚本,出于安全原因。 (Fe有人可能会在Web上某处发布“指令”,试图通过将脚本代码复制并粘贴到控制台来欺骗缺乏经验的用户执行恶意脚本。) – CBroe

+0

好的,但我可以将整个mathjax.js加载到控制台中方式为我添加适应该脚本的行'''src.script =“链接到数学jax”''' –

回答

-1

(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); 
 
    } 
 
})();

+0

这只是我的代码段的复制粘贴 –