移动Twitter,Greasemonkey脚本不起作用

时间:2011-08-31 05:59:55

标签: jquery firefox twitter greasemonkey

我有一个脚本未在移动Twitter上执行的问题。我在很多版本的Firefox上都尝试过它,包括FF5,FF8和FF9。

我使用过最新版本的Greasemonkey。现在我有通用0.9.10的FF9。我在普通的twitter上执行脚本没有任何问题,但在移动设备上它不起作用。也许与HTTPS有关?

这是脚本:

// ==UserScript==
// @name           Twitter Mobile
// @namespace      http://www.test.com
// @include        https://mobile.twitter.com/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

function MainLoop() {

if(typeof unsafeWindow.jQuery == 'undefined') {
    window.setTimeout(MainLoop, 100);
    return;
}

var $ = unsafeWindow.jQuery;

$(document).ready(function(){
          //do something here
});
window.setTimeout(MainLoop, 2000);
}

MainLoop();

我尽力了。另外,检查了@require替代方案并尝试了它们。我不知道为什么拒绝执行。

3 个答案:

答案 0 :(得分:0)

mobile.twitter.com在其响应中返回以下HTTP标头:

X-Content-Security-Policy: allow 'self'; img-src *.twitter.com *.twimg.com maps.google.com data:; media-src *.twitter.com *.twimg.com; style-src *.twitter.com *.twimg.com; frame-ancestors *.twitter.com; script-src *.twitter.com *.twimg.com api-secure.recaptcha.net; report-uri https://mobile.twitter.com/csp_violation_report

此标头定义Content Security Policy。这里的重要部分是script-src *.twitter.com *.twimg.com api-secure.recaptcha.net,这会将此页面上的脚本限制为几个域。无法加载来自其他域的脚本,甚至不能加载GreaseMonkey(如果您打开错误控制台,您将看到相应的警告)。

我建议您将jQuery粘贴到GreaseMonkey脚本中,而不是尝试将其注入网页。这将增加脚本的大小,但至少你可以可靠地使用它 - 你将不再需要等待jQuery加载。

旁注:我猜Twitter正在移动网站上测试CSP,在将其部署到主站点之前影响较小。因此,主站点的脚本可能也会很快停止运行。

答案 1 :(得分:0)

所有MainLoop内容都不是必需的,并且还强制执行错误的jQuery实例。

请执行以下操作:

  1. 切换回Firefox的最新 稳定 版本(目前版本6)。

  2. 删除脚本// ==/UserScript==后的所有内容,但//do something here部分除外。

    1. 请勿使用unsafeWindow.jQuery
    2. 您不需要任何$(document).ready个电话;默认情况下,Greasemonkey会在适当的时间执行。
    3. 删除当前的setTimeout()来电;他们适得其反。

    4. @require指令更改为:

      // @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
      


    5. 粘贴或链接到完整的脚本!当前脚本可能失败主要是因为它正在检查页面上是AJAX-in的内容。
      用这样的代码解决这个问题:

      //--- This handles both page-load delays, and AJAX changes.
      setInterval (function() { checkForTweetbox (); }, 500);
      
      function checkForTweetbox () {
          var tweetbox = document.querySelector ('div.tweet-box textarea');
          if (tweetbox) {
              if (! tweetbox.weHaveProcessed) {
                  tweetbox.weHaveProcessed    = true;
                  alert ('New tweet-box found!');
              }
          }
      }
      

    6. 事实上,这个脚本非常有效:

      // ==UserScript==
      // @name            _mobile twitter
      // @include         https://mobile.twitter.com/*
      // @require         http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
      // ==/UserScript==
      
      setInterval (function() { checkForTweetbox (); }, 500);
      
      function checkForTweetbox () {
          var tweetbox = document.querySelector ('#message_text');
          if (tweetbox) {
              if (! tweetbox.weHaveProcessed) {
                  tweetbox.weHaveProcessed    = true;
                  tweetbox.value              = 'GM\'s jQuery version is: ' + $.fn.jquery;
              }
          }
      }
      

      如果您已登录Twitter移动版,则会在消息框中预加载推文(但不会发送)。

答案 2 :(得分:0)

编辑:正如Brock注意到的,从GreaseMonkey 0.9.0开始,这个答案已经过时了。

@require标记仅在第一次安装脚本时处理。因此,更改@require行不会执行任何操作,因为安装脚本后不会下载任何内容。

另见How can I use jQuery in Greasemonkey?