Skip to content

Instantly share code, notes, and snippets.

@arantius
Last active June 3, 2017 22:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arantius/1157543 to your computer and use it in GitHub Desktop.
Save arantius/1157543 to your computer and use it in GitHub Desktop.
General purpose Greasemonkey Tester
// ==UserScript==
// @name Greasemonkey Tester
// @name:en-US Greasemonkey Tester (en-US)
// @name:fr La Tester Sur Greasemonkey
// @namespace https://github.com/arantius
// @include http://localhost/infinite.php?*
// @match http://localhost/infinite.php*
// @exclude /.*0$/
// @resource R https://gist.github.com/arantius/1157543/raw/data.txt
// @icon http://icons.iconarchive.com/icons/chicho21net/jungle-bells/48/CocoDrilo-icon.png
// @icon http://cdn.androidblip.com/icn/com_betterandroid_icons_creature____34326.png
// @resource icon http://cdn.androidblip.com/icn/com_betterandroid_icons_creature____34326.png
// @require https://gist.github.com/arantius/1157543/raw/require.js
// @version 1.7
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_listValues
// @grant GM_xmlhttpRequest
// @grant GM_deleteValue
// @grant GM_getResourceURL
// @grant GM_getResourceText
// @grant GM_registerMenuCommand
// @grant GM_log
// @grant GM_openInTab
// ==/UserScript==
/*
For this to work as expected, http://localhost/infinite.php should be e.g.:
https://gist.github.com/raw/1157543/infinite.php
Include it anywhere else and it should be ok except the GM_addStyle() test.
*/
try {
eval("dump('eval works.\\n');");
//eval("alert('alert works in eval');");
dump('document references work: ' + document.body + '\n');
dump('document references work: '
+ document.getElementById('style_prover') + '\n');
dump('document.body expando should not work: ' + document.body.expando + '\n');
dump('unsafe document.body expando should work: '
+ unsafeWindow.document.body.expando + '\n');
dump('Should be undefined: ' + window.content_value + '\n');
dump('Should be "content": ' + unsafeWindow.content_value + '\n');
var el = document.evaluate('//*', document, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null);
dump('xpath works: ' + el.singleNodeValue + '\n');
GM_log('GM_log() works.');
console.log('firebug console works.');
GM_addStyle("#style_prover:after { content: 'GM_addStyle() works '; }");
var i = GM_getValue('i', 0);
GM_setValue('i', ++i);
GM_setValue('j', '');
dump('GM_getValue() works: ' + i + '\n');
eval("dump('GM_getValue() from eval (Firefox 5+ only?) works: "
+ "' + GM_getValue('i', 0) + '\\n');");
dump('GM_setValue() works (if it threw no error and "j" shows up):\n');
function listValues() {
var values;
try {
values = GM_listValues();
} catch (e) {
values = 'failure';
dump('\n\n'+e+'\n'+uneval(e)+'\n\n');
}
dump('GM_listValues() works: ' + values + '\n');
dump('There are ' + (values.length) + ' values.\n');
}
listValues();
GM_deleteValue('j');
dump('GM_deleteValue() works (if "j" does not show up):\n');
listValues();
dump('resource url: ' + GM_getResourceURL('R').substring(0, 30) + '...\n');
dump('resource text: ' + GM_getResourceText('R').substring(0, 30) + '...\n');
var oitButton = document.createElement('button');
oitButton.addEventListener('click', function() {
GM_openInTab('http://localhost/');
}, false);
oitButton.appendChild(document.createTextNode('Test GM_openInTab'));
document.body.appendChild(oitButton);
GM_registerMenuCommand(
'GM_rmc works', function(){ dump('GM_rmc really works.\n'); });
GM_xmlhttpRequest({
method: 'GET',
//url: 'http://localhost/',
url: '/img/apache_pb.gif',
onload: function(response) {
dump('GM_xmlhttpRequest works: '
+ response.responseText.substring(0, 30) + '\n');
},
onprogress: function(e) {
dump('gm_xhr onprogress lengthComputable: ' + (e.lengthComputable || '') + '\n');
dump('gm_xhr onprogress loaded: ' + (e.loaded || '') + '\n');
dump('gm_xhr onprogress total: ' + (e.total || '') + '\n');
}
});
function testStrict() {
"use strict";
undeclared = 'value';
}
try {
testStrict();
dump("Strict mode was NOT enforced!\n");
} catch (e) {
dump("Strict mode was enforced!\n");
}
dump('GM_info: ' + uneval(GM_info).substring(0, 120) + '\n');
dump('=======================================================\n');
setTimeout(
function() { dump('setTimeout, function, short delay works.\n') }, 100);
setTimeout(
function() { dump('setTimeout, function, long delay works.\n') }, 5000);
try {
setTimeout("dump('setTimeout, string, short delay works.\\n')", 100);
} catch (e) {
dump('setTimeout, string, short delay FAIL.\n');
}
try {
setTimeout("dump('setTimeout, string, long delay works.\\n')", 5000);
} catch (e) {
dump('setTimeout, string, long delay FAIL.\n');
}
setTimeout(
function(x, y) { dump(x+y) }, 100,
'setTimeout, function, short', ' delay, extra args works.\n');
var img = document.createElement('img');
img.src = GM_getResourceURL('icon');
img.style = 'display: block;'
document.body.appendChild(img);
} catch (e) { dump('api tester: '+e+'\n'+uneval(e)+'\n') }
This is some data.
<html>
<head>
<title>Infinite tester ...</title>
</head>
<body>
<script>
var content_value = 'content';
document.body.expando = 'content body expando';
</script>
<?
$x = 0;
if (isset($_GET['x'])) {
$x = (int)$_GET['x'] + 1;
}
?>
<a href="?x=<?=$x?>">continue ...</a>
<p>http://www.example.com/</p>
<ul>
<li><a href="#1">#1</a></li>
<li><a href="#2">#2</a></li>
<li><a href="#3">#3</a></li>
</ul>
<hr>
<div id="style_prover"><tt>GM_addStyle()</tt> test: </div>
<hr>
</body>
dump("@require works!\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment