Mobile Twitter, Greasemonkey script does not work
I have this problem with a script not executing on mobile twitter. I have tried it on many versions of Firefox includi开发者_如何学运维ng FF5, FF8, and FF9.
I have used the latest versions of Greasemonkey. Right now I have FF9 with GM 0.9.10. I do not have any problems executing the script on the normal twitter but on mobile it does not work. Maybe has something to do with HTTPS?
Here is the script:
// ==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();
I have tried everything possible. Also, checked the @require
alternatives and tried them. I do not know why it refuses to execute.
All that MainLoop
stuff is not required, and also forcing the wrong instance of jQuery.
Please do the following:
Switch back to the latest stable version of Firefox (Currently version 6).
Delete everything after
// ==/UserScript==
in the script except the//do something here
part.- Do not use
unsafeWindow.jQuery
. - You do not need any
$(document).ready
calls; Greasemonkey executes at the proper time by default. - Delete the current
setTimeout()
calls; they are counter productive.
- Do not use
Change the
@require
directive to:// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
Paste or link to the complete script! The current script is probably failing mostly because it's checking for something on the page that is AJAXed-in.
get around that with code like this://--- 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!'); } } }
In fact, this script works perfectly well:
// ==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;
}
}
}
If you are logged into twitter mobile, it will preload a tweet in the message box (but not send).
mobile.twitter.com returns the following HTTP header in its response:
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
This header defines a Content Security Policy. The important part here is script-src *.twitter.com *.twimg.com api-secure.recaptcha.net
, this restricts the scripts on this page to a few domains. Scripts from other domains cannot be loaded, not even by GreaseMonkey (and if you open the Error Console you will see the corresponding warnings).
I suggest that you paste jQuery into your GreaseMonkey script instead of trying to inject it into the web page. This will increase the size of your script but at least you will be able to use it reliably - and you will no longer need to wait for jQuery to load.
Side-note: I guess that Twitter is testing CSP on the mobile site where it has less impact before deploying it on the main site as well. So your script for the main site might also stop working soon.
Edit: as Brock noticed, this answer is obsolete as of GreaseMonkey 0.9.0.
A @require tag is only processed the first time a script is installed. So changing the @require line does not do anything, as nothing will be downloaded after the script has been installed.
See also How can I use jQuery in Greasemonkey?
精彩评论