Difference between revisions of "Greasemonkey Manual:Installing Scripts"

From GreaseSpot Wiki
Jump to navigationJump to search
 
(19 intermediate revisions by 9 users not shown)
Line 1: Line 1:
// @name          Poker Bettor Status
{{Greasemonkey Manual TOC}}
// @namespace      d2jsp
// @include        http://forums.d2jsp.org/topic.php?t=*&f=*
// @include      http://forums.d2jsp.org/user.php?i=*
// @include      http://forums.d2jsp.org/pm.php?c=*
// ==/UserScript==


var TRUSTED = [];
== About User Scripts ==
var ACTIVE = [];
var REGULAR = [];
var WELCHERPB = [];
var WELCHER = [];
var BLACKLIST = [];
var MED = [];


function getElementsByClassName(classname, par){
The purpose of [[Greasemonkey]] is to manage user scripts.
  var a=[];
[[User script]]s allow the ''user'' to control the way they use the web, by customizing it with scripting.
  var re = new RegExp('\\b' + classname + '\\b');
The [https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/ Greasemonkey extension] won't do any good without any scripts installed.
  var els = par.getElementsByTagName("*");
  for(var i=0,j=els.length; i<j; i++){
      if(re.test(els[i].className)){
        a.push(els[i]);
      }
  }
  return a;
};


function resolveBettorType(name){
The first thing an eager user should do is find and install ''(or write!)'' a useful script.
var i;
for(i=0;i<MED.length;i++){
if(name == MED[i]){
return 'med';
}
}
for(i=0;i<BLACKLIST.length;i++){
if(name == BLACKLIST[i]){
return 'blacklist';
}
}
for(i=0;i<TRUSTED.length;i++){
if(name == TRUSTED[i]){
return 'trusted';
}
}
for(i=0;i<WELCHERPB.length;i++){
if(name == WELCHERPB[i]){
return 'welcherpaidback';
}
}
for(i=0;i<REGULAR.length;i++){
if(name == REGULAR[i]){
return 'regular';
}
}
for(i=0;i<WELCHER.length;i++){
if(name == WELCHER[i]){
return 'welcher';
}
}


for(i=0;i<ACTIVE.length;i++){
:* A word on finding [[user script]]s. They may be located anywhere on the internet but you should look first at [[User_Script_Hosting|common hosts]].
if(name == ACTIVE[i]){
return 'active';
}
}
return 'unknown';
};


function createHTML(div,user){
Installation of a script is most often done by clicking a link on a web page.
var br = document.createElement('BR');
<!-- Not in 4.0
var holder = document.createElement('B');
One may also drag-and-drop a local file into the browser window, or optionally use the menu bar [http://support.mozilla.com/en-US/kb/Menu+Reference#Open_File_ File &rarr; Open File...] dialog to open it.
holder.innerHTML += '<a href="http://forums.d2jsp.org/forum.php?f=104"' +
-->
' title="Bar and Pub Games" target="_blank">Poker Status</a>: ';
switch(resolveBettorType(user)){
case 'med':
  holder.innerHTML += '<font color="chartreuse"><b>Mediator</b></font>';
  break;
case 'welcher':
  holder.innerHTML += '<font color="red"><b>Welcher</b></font>';
  break;
case 'active':
  holder.innerHTML += '<font color="purple"><b>Active</b></font>';
  break;
case 'welcherpaidback':
  holder.innerHTML += '<font color="fuchsia"><b>Paid-Back</b></font>';
  break;
case 'trusted':
  holder.innerHTML += '<font color="blue"><b>Trusted</b></font>';
  break;
case 'blacklist':
  holder.innerHTML += '<font color="black"><b>Blacklist</b></font>';
  break;
case 'regular':
  holder.innerHTML += '<font color="orange"><b>Regular</b></font>';
  break;
case 'unknown':
holder.innerHTML += '<font color="gray"><b>Unknown</b></font>';
break;
}


var beforeEle = div.getElementsByTagName('DIV')[0];
:* Any file that ends in '''<code>.user.js</code>''' is a user script.
div.insertBefore(holder,beforeEle);
div.insertBefore(br,beforeEle);
};


function doThePage(){
When navigating to a URL which ends with <code>.user.js</code>, [[Greasemonkey]] will trigger the installation dialog.
var divs = getElementsByClassName('bc1',document);
Note that [[Greasemonkey]] must be [[Troubleshooting_(Users)#Greasemonkey Enabled Status|enabled]] to do so.
var names = document.getElementsByTagName('DT');
var name,str,nameOffset,divOffset;
nameOffset = 0;
divOffset = 0;
for(var i=0;i<divs.length;i++){
if(names[i + nameOffset].innerHTML == 'User Poll'){
nameOffset++;
}
if(window.location.href.indexOf('pm.php?') > 0){
nameOffset = 2;
divOffset = divs.length - 1;
}
if(window.location.href.indexOf('index.php?act=Post&c') > 0){
nameOffset = 1;
}
str = names[i + nameOffset].firstChild;


if(str.innerHTML.indexOf('<') == 0){
== The Installation Dialog ==
str = str.firstChild;
}
str = str.innerHTML;
var idx = str.indexOf('<');
if(idx != -1){
name = str.substring(0,idx)
}else{
name = str;
}
name = name.replace(/ /gi,'');
createHTML(divs[i + divOffset],name.toUpperCase());
if(window.location.href.indexOf('pm.php?') > 0 || window.location.href.indexOf('user.php?i') > 0){
break;
}
}
};


function getNames(){
[[Image:Install-dialog.png|left|thumb|150px|GM Installation Dialog]]
GM_xmlhttpRequest(
{
    method: 'GET',
    url: 'http://pokerlist.angelfire.com/',
    headers:
{
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(response)
{
var str = response.responseText;
var names = str.substring(str.indexOf('StArToFmYBETTORlIsT') + 19,str.indexOf('EnDoFmYBETTORlIsT'));
eval(names);
doThePage();
}
});


};
When navigating to a [[user script]], Greasemonkey will open its installation dialog instead of loading the script like a normal page.
A thumbnail of this dialog is shown to the left.
It displays the name and description of the script as well as the [[include and exclude rules]] and special [[API]]s that apply.
<!-- Not in 4.0!
''Note:'' Greasemonkey must be  [[Troubleshooting (Users)#Greasemonkey Enabled Status|enabled]] to install scripts.
-->


getNames();
;* The Install button
This button will, of course, install the script in question.
Like the Firefox extension installation dialog, this button is disabled for a few seconds to avoid the same potential [http://www.squarefree.com/2004/07/01/race-conditions-in-security-dialogs security vulnerability].
 
;* The Cancel button
This button will cancel the installation of a script.
 
<!-- Not in 4.0
;* The View Script Source button
This button will allow viewing of the source code contained in the script.
At this point, [[Greasemonkey]] has already downloaded the [[user script]] in question to display the name and other details.
 
When a user shows the script source, it displays the temporary file that Greasemonkey has already downloaded depicted in this [[:media:View-source.png|screenshot]].
In this window there is an information bar at the top similar to the Firefox extension installation security warning.
Clicking the install button here will also install the script.
 
-->
Now with some scripts installed, we can open the [[Greasemonkey Manual:Monkey Menu|Monkey Menu]] to list and manage them.

Latest revision as of 15:06, 3 November 2017

Book.png
Greasemonkey Manual
Using Greasemonkey
Installing Scripts
Monkey Menu
Getting Help
User Script Authoring
Editing
Environment
API

About User Scripts

The purpose of Greasemonkey is to manage user scripts. User scripts allow the user to control the way they use the web, by customizing it with scripting. The Greasemonkey extension won't do any good without any scripts installed.

The first thing an eager user should do is find and install (or write!) a useful script.

Installation of a script is most often done by clicking a link on a web page.

  • Any file that ends in .user.js is a user script.

When navigating to a URL which ends with .user.js, Greasemonkey will trigger the installation dialog. Note that Greasemonkey must be enabled to do so.

The Installation Dialog

GM Installation Dialog

When navigating to a user script, Greasemonkey will open its installation dialog instead of loading the script like a normal page. A thumbnail of this dialog is shown to the left. It displays the name and description of the script as well as the include and exclude rules and special APIs that apply.

  • The Install button

This button will, of course, install the script in question. Like the Firefox extension installation dialog, this button is disabled for a few seconds to avoid the same potential security vulnerability.

  • The Cancel button

This button will cancel the installation of a script.

Now with some scripts installed, we can open the Monkey Menu to list and manage them.