为什么 jQuery 不会加载到 Facebook 中? [英] Why will jQuery not load in Facebook?

查看:16
本文介绍了为什么 jQuery 不会加载到 Facebook 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我正在编写一个适用于任何页面的 chrome 扩展程序.

Situation : I am writing a chrome extension that works on any page.

问题:我无法将 jQuery 加载到 Facebook,我想了解发生了什么.

Problem Question : I can not load jQuery into Facebook and I would like to understand what is happening.

假设:Facebook 拥有一些超先进的技术,可以以某种方式检测到两者:

Hypotheses : Facebook possess some ultra advanced tech that somehow detects both :

  1. 当通过 chrome 扩展加载 jQuery 时表面上分离 JSVM 执行上下文,Facebook megamind不知何故知道这个表面上separate JSVM 执行上下文,并阻止它.
  2. jQuery 是通过 script.src 加载并阻塞它 (当我使用通过 HTTPS 提供服务的 Google CDN 而不是方法 2 不起作用的 jQuery,但对于回答).
  1. When jQuery is loaded via a chrome extension in an ostensibly separate JSVM execution context, the Facebook megamind somehow knows about this ostensibly separate JSVM execution context, and blocks it.
  2. that jQuery is loaded via script.src and blocks it (when I used the Google CDN which serves over HTTPS instead of the jQuery one which doesn't method 2 works, but is not sufficient for answer).

数据

我怎么知道 jQuery 没有加载?

I j 在 Chrome 中调出控制台.当我这样做时:

I j to bring up the console in Chrome. When I do :

    > jQuery
    >> ReferenceError : jQuery is not defined.
    > $('body')
    >> Error : Tried to get element "body" but it is not present on the page.

我如何尝试在 facebook 中加载 jQuery?

方法 1(必需但失败):

通过 manifest.json 文件中的以下代码:

Via the following code in the manifest.json file :

"content_scripts"         :   [
                                  {
                                    "matches"   : ["<all_urls>"],
                                    "js"        : [ 
                                                    "javascript/jq/jquery-1.9.1.min.js",                                            
                                                    "javascript/jq/non-standard.js"
                                                  ],
                                    "all_frames": true // (or false, same failure)
                                  } 
                              ]

方法二(有效,但不够用):

通过此 SO 答案中描述的方法(将 jQuery 加载到控制台),修改为允许正确的协议:

Via the method described in this SO answer (load jQuery into console), modified to permit the correct protocol :

    var jq = document.createElement('script');
    jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(jq);
    jQuery.noConflict();

总结

假设 1 似乎不太可能,因为覆盖 Web 浏览器的单独执行上下文将是一个主要的安全漏洞(破坏沙箱),并且不太可能受到制裁.因此,我可能是偏执狂,忽略了显而易见的事情,希望你们中的一个人会看到.

Hypothesis 1 seems very unlikely, because over-riding the separate execution contexts of a web browser would be a major security vulnerability (break that sandbox), and not likely to be sanctioned. Therefore, I am probably being paranoid and overlooking the obvious, which hopefully one of you will see.

附录(其他相关代码)

所有非标准.js:

    $.fn.in_groups_of = function( countPerGroup ) {
        var groups = [], offset = 0, $group;
        while ( ($group = this.slice( offset, (countPerGroup + offset) )).length ) {
            groups.push( $group );
            offset += countPerGroup;
        }
        return groups;
    };

更多 manifest.json :

"manifest_version"        :   2,
"permissions"             :   [
                                  "http://*/",
                                  "https://*/",
                                  "tabs",
                                  "storage",
                                  "unlimitedStorage"
                              ],

推荐答案

Chrome 控制台似乎无权访问内容脚本的执行上下文.

The Chrome console does not appear to have access to the content script's execution context.

错了,确实如此.你需要看正确的地方:

Wrong, it does. You need to look at the correct place:

之前的截屏显示,Chrome 开发者工具的控制台选项卡底部有两个下拉框,可用于更改开发者工具控制台的执行环境.
左侧可用于更改框架上下文(顶部框架,因此 iframe,...),右侧可用于更改脚本上下文(页面,内容脚本,...).

The previous screencast shows that the Console tab of the Chrome developer tools has two dropdown boxes at the bottom, which can be used to change the execution environment for the developer tools' console.
The left side can be used to change the frame context (top frame, so iframe, ...), and the right side can be used to change the script context (page, content script, ...).

这篇关于为什么 jQuery 不会加载到 Facebook 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆