Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Insurrectum
Nov 1, 2005

Did anyone else's greasemonkey's scripts stop working with Firefox 4? I'm not exactly what was broken by the change, but one of my scripts isn't working now.

Adbot
ADBOT LOVES YOU

geeves
Sep 16, 2004

epswing posted:

Use parseInt!

code:
var zipNum = parseInt(zipcode);
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
Careful with parseInt on zipcodes!

It's better to check for field length and/or use a regex with zip codes because using parseInt could lead to unintended consequenses for codes that start with zero. http://www.w3schools.com/jsref/jsref_parseInt.asp

so instead, something like:

code:
var patt10 = /\d\d\d\d\d-\d\d\d\d/i;
var patt5 = /\d\d\d\d\d/i;
if (10 === zipcode.value.length && zipcode.value.match(patt10)) {
  // valid 5 + 4 zipcode.
} else if (5 === zipcode.value.length && zipcode.value.match(patt5)) {
  // valid 5 digit zipcode.
}
VVVV Much more elegant, regex aren't my strong suit

geeves fucked around with this message at 20:32 on Mar 23, 2011

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

geeves posted:

Careful with parseInt on zipcodes!

It's better to check for field length and/or use a regex with zip codes because using parseInt could lead to unintended consequenses for codes that start with zero. http://www.w3schools.com/jsref/jsref_parseInt.asp

so instead, something like:

code:
var patt10 = /\d\d\d\d\d-\d\d\d\d/i;
var patt5 = /\d\d\d\d\d/i;
if (10 === zipcode.value.length && zipcode.value.match(patt10)) {
  // valid 5 + 4 zipcode.
} else if (5 === zipcode.value.length && zipcode.value.match(patt5)) {
  // valid 5 digit zipcode.
}

There is no need to check length or use two regex:

code:
function butt(zip) {
  var pat = /^\d\d\d\d\d(-\d\d\d\d)?$/i
  if (zip.match(pat)) {
    alert(zip + " is good");
  }
}

butt('cow'); //fail
butt('12345'); // alerts
butt('123451111'); //fail
butt('12345-9');//fail
butt('44444-1119991');//fail
butt('44444-1111');  // alerts

hayden.
Sep 11, 2007

here's a goat on a pig or something
Great, thank you everyone!

VodeAndreas
Apr 30, 2009

I'm not sure if this is a Firefox 4.0 or a Greasemonkey/Javascript issue but being too incompetent to find vaguely relevant threads other than this I figured I'll give you a try:

A while ago in the old Greasemonkey thread that used to be somewhere I grabbed someone's script that's meant to open all the bookmarked threads you have with new posts from your control panel which worked perfectly, however today when I installed Firefox 4.0 it broke and I'm not sure why. I'm assuming the structure of the SA control panel page hasn't changed at all so what might have caused this to die all of a sudden?

The button still appears but when clicked only opens the first thread with new posts instead of all.

code:
// ==UserScript==
// @name          Something Awful Open Updated Threads In Tabs
// @include     [url]http://forums.somethingawful.com/usercp.php*[/url]
// @description   Adds a button on the User Control Panel that opens any updated threads into new tabs
// ==/UserScript==

function handleClick(e) {
   // Remove the button
   element = document.getElementById("openall");
   element.parentNode.removeChild(element);

   // Open all updated threads in tabs
   var got = document.evaluate('.//*[@class="count"]', document, null, XPathResult.ANY_TYPE, null);
   while (a = got.iterateNext())
     GM_openInTab(a.href);
}

// Add the button
var title = document.evaluate('.//*[@class="title"]', document, null, XPathResult.ANY_TYPE, null).iterateNext();
var dummyDiv = document.createElement('div');
dummyDiv.innerHTML = '<div id="openall" style="float:right;font-size:12px;background:#F7F7F7; padding: 2px;'
  + 'border:1px solid #D5D5D5;color:#555;cursor:pointer;">Open updated threads into new tabs</div>';
title.insertBefore(dummyDiv.firstChild, title.firstChild);
document.getElementById("openall").addEventListener('click', handleClick, false);

All Hat
Jul 11, 2008

He that is without int among you, let him first cast a long

Not sure if this is exactly the same, but did you see this post:

http://forums.somethingawful.com/showthread.php?threadid=3070034&userid=0&perpage=40&pagenumber=21#post386911133

VodeAndreas
Apr 30, 2009

I did not see that post, and that does exactly the same thing as the one I had, but currently works! And has a text link instead of a slightly fancier button, but who cares about that.

Don't care what was wrong with the one in my post anymore as this one works just as good. :P

cheese eats mouse
Jul 6, 2007

A real Portlander now
I'm really stumped on this one. I'm using the superfish nav bar, but instead of hiding the subnav on the mouse out I want it to stay displayed. The subsub nav can stay the same. Is there a quick fix? I've tried from my very little knowledge deleting different functions and nothing seems to help. Google has failed me as well.

Example: http://sjenningsdesign.com/theacademy/navtest.html
Got the code here: http://users.tpg.com.au/j_birch/plugins/superfish/

code:
;(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			//Thanks to Steve Kelley for this
			$('li:has(ul)',this)(over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

cheese eats mouse fucked around with this message at 21:26 on Mar 24, 2011

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



I'm not great at CSS, but I think that's what's doing it and not the JS - especially since when I open your example page in FF, I just get two JavaScript errors that indicate the plugin code isn't running:
code:
jQuery is not defined
[url]http://sjenningsdesign.com/theacademy/js/superfish.js[/url]
Line 120

$("ul.sf-menu").superfish is not a function
[url]http://sjenningsdesign.com/theacademy/navtest.html[/url]
Line 15
edit: you do need to put jQuery plugins after the jQuery link in your header

Munkeymon fucked around with this message at 23:10 on Mar 24, 2011

cheese eats mouse
Jul 6, 2007

A real Portlander now

Munkeymon posted:

I'm not great at CSS, but I think that's what's doing it and not the JS - especially since when I open your example page in FF, I just get two JavaScript errors that indicate the plugin code isn't running:
code:
jQuery is not defined
[url]http://sjenningsdesign.com/theacademy/js/superfish.js[/url]
Line 120

$("ul.sf-menu").superfish is not a function
[url]http://sjenningsdesign.com/theacademy/navtest.html[/url]
Line 15
edit: you do need to put jQuery plugins after the jQuery link in your header

Fixed the plugins. Still works the same, but I'll play with the CSS more.

def snow leppard
Sep 12, 2010

When should SwingUtilities.invokeLater ( Runnable ) be used?

Wrong thread.

def snow leppard fucked around with this message at 03:44 on Mar 25, 2011

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Clank posted:

When should SwingUtilities.invokeLater ( Runnable ) be used?

It should be used in the Java questions thread. I get confused when they are right next to each other in the thread list too. :D

def snow leppard
Sep 12, 2010

Lumpy posted:

It should be used in the Java questions thread. I get confused when they are right next to each other in the thread list too. :D

damnit, sorry.

cheese eats mouse
Jul 6, 2007

A real Portlander now

cheese eats mouse posted:

Fixed the plugins. Still works the same, but I'll play with the CSS more.

OK so even removing any instances of :hover in the CSS just removes the color styling. I'm hoping it's not in the JS code.

cheese eats mouse fucked around with this message at 17:06 on Mar 25, 2011

dark_panda
Oct 25, 2004

Lumpy posted:

There is no need to check length or use two regex:

code:
function butt(zip) {
  var pat = /^\d\d\d\d\d(-\d\d\d\d)?$/i
  if (zip.match(pat)) {
    alert(zip + " is good");
  }
}

You can further condense that regexp to /^\d{5}(-\d{4})?$/ .

Boz0r
Sep 7, 2006
The Rocketship in action.
I'm trying to learn javacsript by studying code and I'd like to know what the following line does:

code:
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.";
I don't know what the ? and : signs do.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Boz0r posted:

I'm trying to learn javacsript by studying code and I'd like to know what the following line does:

code:
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.";
I don't know what the ? and : signs do.

It's a ternary conditional.

Steviepoofs
Jan 4, 2011
I am trying to change text in one cell of a table based on a selection of a drop down. The data is pulled from a database to populate the select menu. Here is what I have:

code:
<tr>
<td><select name=\"race\" onchange=\"document.getElementById('ExtraInfo').firstChild.nodeValue = this.options[this.selectedIndex].extrainfo\">
<option selected=\"selected\" value=\"\">Select Race</option>\n";
$sql = mysql_query("SELECT * FROM races");$i=1;
while ($row = mysql_fetch_array($sql)){
	$output .= "<option value=\"$i\" ExtraInfo=\"{$row['des']}\">{$row['race']}</option>\n";
	$i++;
}
$output .= "</select></td>
<td><div id=\"ExtraInfo\">Select Race</div></td>
</tr>
When a selection is made the div tag spits out "undefined".

The code above is part of a PHP script (thus the \")

Any help would be greatly appreciated.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Steviepoofs posted:

I am trying to change text in one cell of a table based on a selection of a drop down. The data is pulled from a database to populate the select menu. Here is what I have:

code:
<tr>
<td><select name=\"race\" onchange=\"document.getElementById('ExtraInfo').firstChild.nodeValue = this.options[this.selectedIndex].extrainfo\">
<option selected=\"selected\" value=\"\">Select Race</option>\n";
$sql = mysql_query("SELECT * FROM races");$i=1;
while ($row = mysql_fetch_array($sql)){
	$output .= "<option value=\"$i\" ExtraInfo=\"{$row['des']}\">{$row['race']}</option>\n";
	$i++;
}
$output .= "</select></td>
<td><div id=\"ExtraInfo\">Select Race</div></td>
</tr>
When a selection is made the div tag spits out "undefined".

The code above is part of a PHP script (thus the \")

Any help would be greatly appreciated.

You can't just tack on made up attributes on an HTML tag and expect them to be accessible in that way. You'll need to parse the OPTION for the attribute you made up either by looping through the attributes collection, or by using getAttribute() but I forget what browsers support that (I think most / all, but check before you use it.)

Lumpy fucked around with this message at 22:30 on Mar 27, 2011

Steviepoofs
Jan 4, 2011

Lumpy posted:

You can't just tack on made up attributes on an HTML tag and expect them to be accessible in that way. You'll need to parse the OPTION for the attribute you made up either by looping through the attributes collection, or by using getAttribute() but I forget what browsers support that (I think most / all, but check before you use it.)

Thank you for getting me on the right path...
code:
<select name=\"race\" onchange=\"document.getElementById('ExtraInfo').firstChild.nodeValue = this.options[this.selectedIndex].getAttribute('extrainfo')\">
Works

NotShadowStar
Sep 20, 2000
Is jsdoc-toolkit still the best/only game around for inline documentation?

smug forum asshole
Jan 15, 2005
YUI Doc looks pretty good to me. I don't use it but it was suggested in a book about Javascript Patterns I sorta read a few months ago.

Baggy_Brad
Jun 9, 2003

THUNDERDOME LOSER
I have a pondering about javascript and captchas and probably spambots more than anything.

Can you add a captcha to a form via JS? Essentially have a script that detects each submit button and overwrites the events to require you to pass a captcha in order to submit?

I mean, obviously you can for human users but what I'm wondering is how do spambots work? Do they run as an agent themselves that has to deal with all the javascript? Or are they just scraping the page for form names/fields and then posting data to the form in a way that a dynamically generated captcha would be avoided anyway.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

smug forum rear end in a top hat posted:

YUI Doc looks pretty good to me. I don't use it but it was suggested in a book about Javascript Patterns I sorta read a few months ago.

YUI Doc is pretty good, definitely recommend you check out the Dana theme for it if you go that route.

Whole Milk
Jan 30, 2011

by Y Kant Ozma Post
I'm trying to get a row of images to scale down to fit the page if the window size is too small for them to all appear on one line without wrapping. So I'm guessing I need a function to find the width of the page, then another one to scale each image down by a proportional amount? I'm a huge newbie to javascript so don't hesitate to make your post as idiot friendly as possible :negative:

Knackered
Jul 29, 2006

Whole Milk posted:

I'm trying to get a row of images to scale down to fit the page if the window size is too small for them to all appear on one line without wrapping. So I'm guessing I need a function to find the width of the page, then another one to scale each image down by a proportional amount? I'm a huge newbie to javascript so don't hesitate to make your post as idiot friendly as possible :negative:

I threw together this quick page to show you it in action.

Just look at the source code and comments, it should be fairly self explanatory, if you have any questions just give me a shout.

http://www.iamquaid.com/~knackered/img_resizer/

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
For a project, I'm supposed to make a page that cannot be exited.

I thought I could accomplish this through the onunload and onbeforeunload event handlers but all that accomplishes is just calling the page but doesn't stop from leaving (even though I did the simple redirect code of:
code:
function redirect()
{
	var href = window.location.href;
	window.location = href;
}
which should have the page redirect to itself (and it appears to work when just clicking a button). I'm at a loss for what I'm supposed to be doing here unless I'm supposed to use iFrames or something.

N.Z.'s Champion
Jun 8, 2003

Yam Slacker

Baggy_Brad posted:

Or are they just scraping the page for form names/fields and then posting data to the form in a way that a dynamically generated captcha would be avoided anyway.
This, they usually don't run JavaScript (like search engine bots).

KuruMonkey
Jul 23, 2004
Baggy_Brad + Master_Odin:

you are both going to encounter the same fundamental issue with Javascript...

NOTHING you write as javascript can be guaranteed to be run at all on the client side, and if it is run, you cannot be sure it was not tampered with before being run.

So you cannot do client side security validation, you MUST redo it server side.

Also Master_Odin; what you're trying is, in actual practice, so obnoxious and so at odds with the fundamental workings of a web browser, that I would hope any browser worth its salt has specific checks on the JS engine to stop you from doing it.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

KuruMonkey posted:

Also Master_Odin; what you're trying is, in actual practice, so obnoxious and so at odds with the fundamental workings of a web browser, that I would hope any browser worth its salt has specific checks on the JS engine to stop you from doing it.
Yeah, I know. But like I said, it's for a project for my Networking class and so whenever I try and google up something, I keep getting answers like this whenever someone asks this question. I'd never actually use this in practice as I'm sure I'd get quite a few angry people on my hands. The other parts of the project were making unlimited pop-ups appear (new windows) and having endless notifications.

The browser in question though is Firefox 3.6.x also.

epswing
Nov 4, 2003

Soiled Meat

Master_Odin posted:

For a project, I'm supposed to make a page that cannot be exited.

Master_Odin posted:

The other parts of the project were making unlimited pop-ups appear (new windows) and having endless notifications.

What the gently caress kind of project is this. :colbert:

spiritual bypass
Feb 19, 2008

Grimey Drawer
Browser Domination Sado-Masochism

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

epswing posted:

What the gently caress kind of project is this. :colbert:

http://crypto.stanford.edu/cs142/projects/2/project2.html

it's 1c. Not my school, but the exact same project document nevertheless.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

epswing posted:

What the gently caress kind of project is this. :colbert:

Apparently the advertising industry has donated enough money to their school to influence the curriculum, and they are hard at work churning out the next generation of annoying browser ad coders?

It's actually a pretty cool project for a security-type class though.

spiritual bypass
Feb 19, 2008

Grimey Drawer
An infinite loop will stop FF3 in its tracks for a couple minutes iirc...

KuruMonkey
Jul 23, 2004

Master_Odin posted:

http://crypto.stanford.edu/cs142/projects/2/project2.html

it's 1c. Not my school, but the exact same project document nevertheless.

Well, this agrees with my thought that its specifically being prevented for security:

http://stackoverflow.com/questions/2357625/changing-window-location-href-in-firefox-in-response-to-an-onunload-event

Which marries up with what you would hope (user intent trumps programmer intent when they conflict at the client side).

It also marries up with a little bit of experimenting I did here.

This also has some info on how onbeforeunload is apparently not a normal event anymore:

http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-cha

Its possible your course hasn't been updated in the face of updates to browser secuturity (also possibly your school is blindly reusing another school's resources, and has only the old, obsolete material)


Time to ask your tutor for their exemplar answer to this problem.
For extra credit; post it here. (whether or not it works; if it doesn't work, post your tutor's explanation of that...)

Edit; there's problably a version of FF before which it worked. Its before 3.6.16, I know that...

KuruMonkey fucked around with this message at 17:26 on Apr 5, 2011

NotShadowStar
Sep 20, 2000

rt4 posted:

An infinite loop will stop FF3 in its tracks for a couple minutes iirc...

I don't think an infinite loop should kill any browser as long as the loop is doing something, otherwise things like Sproutcore that have a run loop wouldn't work.

This works fine, but modern browsers have a 'stop alerting me fuckwad' checkbox these days. Replacing alert with console.log pretty much causes the tab or browser to be unusable.

a = "he's climbin in your windows-he's snatchin your people up-tryna rape em so y'all need to-hide your kids, hide your wife-hide your kids, hide your wife-hide your kids, hide your wife-and hide your husband-cuz they're rapin errbody out here-you don't have to come and confess-we're lookin for you-we gon find you-we gon find you-so you can run and tell that,-run and tell that-run and tell that, homeboy-home, home, homeboy".split("-"); for(var i = 0; i<=a.length; i++) { alert(a[i]); if (i === a.length) { i = 0;}}

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

KuruMonkey posted:

Time to ask your tutor for their exemplar answer to this problem.
For extra credit; post it here. (whether or not it works; if it doesn't work, post your tutor's explanation of that...)
He gave me:
code:
      setTimeout("window.location.reload();", 15);
      window.onbeforeunload = null;
Except it doesn't work and shouldn't (as window.onbeforeunload=null doesn't actually prevent the user from leaving the page?) or am I apparently just horribly inept at actually copying and pasting code? :confused:

edit: It works in a "if you have poo poo timing 800 times you won't leave the page" but after just randomly clicking a link, I leave the page like 8 out of 10 times.

Master_Odin fucked around with this message at 19:52 on Apr 5, 2011

KuruMonkey
Jul 23, 2004

Master_Odin posted:

He gave me:
code:
      setTimeout("window.location.reload();", 15);
      window.onbeforeunload = null;
Except it doesn't work and shouldn't (as window.onbeforeunload=null doesn't actually prevent the user from leaving the page?) or am I apparently just horribly inept at actually copying and pasting code? :confused:

edit: It works in a "if you have poo poo timing 800 times you won't leave the page" but after just randomly clicking a link, I leave the page like 8 out of 10 times.

Well; credit where its due; works for me in Safari

code:
<!doctype html>
<html>
<head>
<title>shamefulness</html>
</head>
<body>
<h2>Welp</h2>
<a href="http://www.google.co.uk">Leave</a>
<script language='javascript' src='/javascript/jquery.js'></script>
<script language='javascript'>
    $(document).ready(function() {
        
        window.onbeforeunload = function() {
            alert('trap sprung');
           setTimeout("window.location.reload();", 15); 
           window.onbeforeunload = null; 
        }
    
    });
</script>
</body>
</html>
Obviously you'd need to replace or supplant jquery.

Adbot
ADBOT LOVES YOU

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

KuruMonkey posted:

Well; credit where its due; works for me in Safari

code:

Obviously you'd need to replace or supplant jquery.
What weird functionality as it appears to work perfectly after I refreshed the page a couple times, even though at first it did allow the "Getting Started" page to go through while nothing else.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply