disable input field

in Ideas and script requests
Subscribe to disable input field 26 posts, 4 voices



bernte User
FirefoxWindows

hi guys... i have a litte question.. since 3 days i try to write a userscript for greasemonkey with no result :(

first for better understanding
i can only use greasemonkey WITHOUT jquery!
the pc is offline (security reason)
firefox version is 2.0.0.11 and not updateable...
greasemonkey version is 0.8.2
the userscript is for localhost

i want to disable an existing input-field with the name "prio"

the code looks like this: http://jsfiddle.net/gv3XF/5/
and i want it like this: http://jsfiddle.net/gv3XF/6/

in firefox 2.0.0.11 i recieve the error

Error: document.querySelector is not a function
Source File: file:///G:/FirefoxPortable2.0.0.11/Data/profile...
Line: 7

in firefox 10 the same code works only on jsfiddle but not on localhost and i recieve the error

Error: oldInput is null
Source File: file:///C:/Dokumente%20und%20Einstellungen/Admi...
Line: 8

the code looks this:

// ==UserScript==
// @name test
// @include *
// @grant GM_getValue
// ==/UserScript==

var oldInput = document.querySelector ("input[name=prio]");
oldInput.setAttribute ("disabled", "disabled");

any ideas what here is the problem?
or maybe has someone an other idea to disable the input-field :D

thanks

best regards
bernte

 
Cletus Scriptwright
FirefoxWindows

Reason for error in FF 2.x:
The methods querySelector and querySelectorAll were introduced in v3.5 (per MDN docs), therefore those won't work on 2.x.

Reason for error in FF 10:
2 reasons:

  • You aren't checking to see if it actually found the element before you start modifying it.
  • Your quotes are off on your querySelector. As a general rule of thumb, keep all strings in single quotes (') and then for the query, all attributes should be double quotes.

Try this instead for FF10+:

var oldInput = document.querySelector('input[name="prio"]');
if (oldInput) {
	// Only set the attribute if it actually found the element.
	oldInput.setAttribute('disabled', 'disabled');
}

or this for FF2.x:
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
	var oldInput = inputs[i];
	if (oldInput.getAttribute('name') === 'prio') {
		oldInput.setAttribute('disabled', 'disabled');
		break;
	}
}

I seriously recommend that you upgrade your Firefox to the latest version and your Greasemonkey to the latest as well. (FFv15.0 / GMv1.0 as of today)

Edit: Fixed typo in code.

 
ComSwitch Scriptwright
FirefoxX11

// create Attribute "disabled and set =disabled;
var disabled = document.createAttribute("disabled");
disabled.nodeValue = "disabled";

// find first element with name prio
var inp= document.getElementsByName("prio")[0];
// set Attribute to it
inp.setAttributeNode(disabled);

really that does not work on FF 1.0 ?
http://jsfiddle.net/Q8MER/

 
bernte User
FirefoxWindows

hi Cletus..

i need only the code for FF2.x i know that we have to update.. but it is not possible! :( the softwarecompany build the system for this browser!
we tried also with portable some new versions but the software wont start and is full of bugs!

so i need only a code for FF2.x :D

your code says: syntax error on line 8
foreach (var i = 0; i < inputs.length; i++) {

thanks for the quick response ;)

 
ComSwitch Scriptwright
FirefoxX11

sems foreach should be only a simple "for"

 
bernte User
FirefoxWindows

sorry for posting again.... can't edit the post..

hi Comswitch...
your code works but it gives me an also an error message in the console:

inp has no propertyies on line 14
inp.setAttributeNode(disabled);

 
ComSwitch Scriptwright
FirefoxX11

Check that the input can found on your site.
var inp= document.getElementsByName("prio")[0];
Maybe, at the time of script there is no name=prio?
I found on internet http://www.webmasterworld.com/forum91/4716.htm Pherhaps inp is null, pls check that.
But first start your script since the main site has finished loading?
wrap that around:

window.addEventListener("load", function(e) {
 startmydisablefunction(); 
},false);

Is the input field in HTML or is it created on the fly by a javascript, your softwareprodukt?
Then you should delay your script (500-1000ms) after site loading is finished.

 
bernte User
FirefoxWindows

the code @ jsfiddle was just a small code
the original is:

#tr valign="top"#
	#td "="" align="left"#Prio: #/td#
	#td align="left"##input maxlength="10" onfocus="setColoredField(this)" name="prio" accesskey="p" value="270.0" size="7" type="text"##/td#
#/tr#

have to edit the code.. replaced < and > with # because code wasn't visible

i don't know what you mean with "But you first start your script since the main site is finised with loading?" my english is not the best :D
your code snippet works! i load the site and the prio box is disabled. but i have in the error console the inp has no properties message

edit: the input field is normaly generated like the sourcecode i wrot above.. the problem is that i may not edit the sourcecode (license)
i just can manipulate! and i just wanna disable the input field for manipulation :D

 
ComSwitch Scriptwright
FirefoxX11

I have understood you can get it working, but you get an error.
try var inp= document.getElementsByName("prio")[0];
we hope there is only one element with name="prio".
Sorry FF1.0 und FF2.0 is a long time ago, I cant remember me well.

 
Cletus Scriptwright
FirefoxWindows

bernte wrote:
your code says: syntax error on line 8
Bah... that should be a for not foreach. Too many languages in my head at once. I guess that's what I get for not testing it... :P I've edited the code above. Try it again.

ComSwitch wrote:
But first start your script since the main site has finished loading?
As this is in a userscript, you don't need to add a listener for window.load (GM runs scripts at DOMContentLoaded by default, which is faster than window.load.)

 
bernte User
FirefoxWindows

@ComSwitch

i think your script is fine... i maybe found the error.. i have the test-site with the priobox in an tab.. and got an tab with this thread.. i think the error comes when i change the tabs because my rule says

// @include *

which means search on all sites right? maybe i can that edit by set the ip for the *

@ Cletus

your code works fine too!!!! :D thanks for helping!

 
bernte User
FirefoxWindows

hi guys.. first i wanna say thank you again! both codes work perfect!
but i have a new problem :D

first take a look at this: http://jsfiddle.net/gv3XF/8/

is it possible to modify the code?
i have 1 access-point for many users. but i want that 5 users are able to change the prio
you can see @ jsfiddle that the username is in the footer!

is it possible to do that in javascript with the code above for FF2.x?

example:
if USER (from footer) == JOHN || LUKE || JEFF || MAX || ANDY
do nothing

else
disable PRIO BOX (from content)

best regards
bernte

 
Cletus Scriptwright
FirefoxWindows

Here you go, http://jsfiddle.net/gv3XF/9/

// List of users.
var users = [
    'John',
    'Luke',
    'Jeff',
    'Max',
    'Andy'
];
// true = accepts only "John", false = accepts "John" or "john".
var caseSensitive = true;


// Creates the RegExp.
var reg = new RegExp('\(User: (' + users.join('|') + ')\)', ((caseSensitive) ? '' : 'i' ));
// Gets footer text.
var userStr = document.getElementById('footer').lastChild.textContent;

// If username was NOT in the list, disable priority input.
if (!reg.test(userStr)) {
    // Gets all inputs.
    var inputs = document.getElementsByTagName('input');
    // Loops through inputs.
    for (var i = 0; i < inputs.length; i++) {
        var oldInput = inputs[i];
        if (oldInput.getAttribute('name') === 'prio') {
            // Input found, disable it.
            oldInput.setAttribute('disabled', 'disabled');
            break;
        }
    }
}

Just add the users to the list. Feel free to change the caseSensitive variable if needed.

 
bernte User
FirefoxWindows

hi Cletus...

your code looks great!
i made the id="footer" just for jsfiddle so i recieve the error: document.getElementById("footer") has no properties.

sorry that was my fault :(

i replaced the footer with the original code.. i have only a class="user"

updated version: http://jsfiddle.net/gv3XF/10/

edit: and getElementsByClassName doesn't work in FF2.x ... damn :(

 
ComSwitch Scriptwright
FirefoxX11

a bit of topic: you want do security by GM Script, but what is the user delete or edit your script?

why not this way: link deleted

edit: jsfiddle link deleted

 
bernte User
FirefoxWindows

@ComSwitch
the User can't delete or edit the script. i have a cool addon for firefox which don't allow to edit/disable addons (password protection)

with your code i recieve the error: document.getElementById("footer") has no properties Line: 16

i have updated my version (http://jsfiddle.net/gv3XF/10/). i don't have the id "footer" anymore. it was my fault! sorry for that :(

 
ComSwitch Scriptwright
FirefoxX11

What do I have to do, that the user can not edit the user.js file, and he can not deaktivate GM.
//I fogott to update jsfidle.
 var str="";
 var userStr="";
 var elm=document.getElementsByTagName("td");
for (var i = 0; i < elm.length; i++) {
 str+="i="+i+" ";
 if(elm[i].getAttribute("class")=="user"){
  str+="Found:"+elm[i].innerHTML;
  userStr= elm[i].lastChild.textContent;
 }
 str+="
"; } document.write(str+" "+userStr);
I expect that you not only copy code,you should understood and edit by your own.
 
bernte User
FirefoxWindows

you need 2 to do some changes.. its very easy..
i can write an tutorial for you if you like.. but i need some time. is that ok?
You scratch my back and I'll scratch yours. :D :D

sorry ComSwitch..
i'm not that coder :/ i'm more special to html and css :D

and your code i don't understand :D i see what you did but can't finish it :(

 
ComSwitch Scriptwright
FirefoxX11

Pls describe it in a short way. I can deaktivate my monkey and I can edit the monkey script, I can deaktivate a script if I have more then one which should work on a page.

if(elm[i].getAttribute("class")=="user") if is tru it is a < td class="user">, and you found the < td> tag, I throught that is what you want.

http://jsfiddle.net/ppGe8/2/

 
bernte User
FirefoxWindows

ok.. here we go!

1. Press right-mouse on greasemonkey logo and click customize (right side of google search window)
2. click and drag greasemonkey-logo into the openend customize window.
3. Press ok and the logo is away! (you can't edit/enable greasemonkey from here anymore)

4. install Menu Editor for Firefox (https://addons.mozilla.org/en-US/firefox/addon/...)
5. after restart go to Menu Editor options (Tools -> Add-ons)
6. edit menu -> Tools
7. Click on Greasemonkey and press Hide. (and all other you like/need)
8. Press OK. (now you can't edit/enable greasemonkey from here anymore)

9. install Pulblic Fox for Firefox (https://addons.mozilla.org/en-US/firefox/addon/...)
10. after restart go to Public Fox options (Tools -> Add-ons)
11. mark the following settings: Lock Add-ons windows, Lock Firefox options, Lock 'about:config' settings-page, Lock Toolbar Customization (you can disable more if you want)
12. Set the "Lock Password".
13. press OK.
14. restart Firefox

15. Have FUN :D

if you want to edit greasemonkey scripts you can do that over Tools -> Add-ons -> User Scripts
if you want to add new script you have to unhide how described it at 4. - 8.

hope you like it

 
bernte User
FirefoxWindows

the only thing i see at ff10.x @http://jsfiddle.net/ppGe8/2/ in the preview box is

i=0 Found:  Logout  (User: John)
i=1
i=2
 (User: John) 

the inputs are gone :/

when i try it in ff2.x the browser crashes

 
ComSwitch Scriptwright
FirefoxX11

Thank you, sounds complicated, but seems you understood what you do.

you wrote
...inputs are gone...

You asked to finde the td tag with class="user", the example show how you could get your information.

 
Cletus Scriptwright
FirefoxWindows

Here ya go:

// List of users.
var users = [
    'John',
    'Luke',
    'Jeff',
    'Max',
    'Andy'
];
// true = accepts only "John", false = accepts "John" or "john".
var caseSensitive = true;


// Creates the RegExp.
var reg = new RegExp('^Logout (' + users.join('|') + ')$', ((caseSensitive) ? '' : 'i' ));

// Gets all links and loops through them.
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
	if (links[i].hasAttribute('title')) {
		var title = links[i].getAttribute('title');
		// Check if link is the logout link.
		if (title.indexOf('Logout') < 0) {
			// Wrong link, try the next one.
			continue;
		}
		// Check if username in list, if so, exit.
		if (reg.test(title)) {
			break;
		}
		// Username not in list, disable priority input.
		else {
			// Gets all inputs and loops through them.
			var inputs = document.getElementsByTagName('input');
			for (var j = 0; j < inputs.length; j++) {
				var oldInput = inputs[j];
				if (oldInput.getAttribute('name') === 'prio') {
					// Input found, disable it.
					oldInput.setAttribute('disabled', 'disabled');
					break;
				}
			}
		}
	}
}

I am just looping through the links looking for a title that contains "Logout USER", and then acting on that.

 
bernte User
FirefoxWindows

yeahhh cletus that works!
TYVM!!!!! :D

 
Cletus Scriptwright
FirefoxWindows

No problem! Feel free to PM me if you need any help.