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
epswing
Nov 4, 2003

Soiled Meat

geeves posted:

I admit I don't like the way it looks. Perhaps because I would never do that in Java.

Do you enjoy writing extra code? Don't name temporary variables if you don't have to.

Edit: Actually, I don't know what you're talking about, because this is perfectly acceptable Java:

code:
public static Person CreatePerson(String name, int age) {
    return new Person(
        name.trim(),
        Math.max(age, 0)
    );
}
To me, that's way better than

code:
public static Person CreatePerson(String name, int age) {
    name = name.trim();
    age = Math.max(age, 0);
    Person p = new Person();
    p.setName(name);
    p.setAge(age);
    return p;
}

epswing fucked around with this message at 15:23 on Jan 5, 2011

Adbot
ADBOT LOVES YOU

Munkeymon
Aug 14, 2003

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



Lumpy posted:

Javascript will add semi-colons for you. And that's a Bad Thing.

A new guy at my company uses GNU style brace formatting and forgets semicolons all the time. When writing Javascript. Yeah, everything you just thought and probably more.

spiritual bypass
Feb 19, 2008

Grimey Drawer
Hoo boy, time to implement a coding standard!

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Munkeymon posted:

A new guy at my company uses GNU style brace formatting and forgets semicolons all the time. When writing Javascript. Yeah, everything you just thought and probably more.

http://www.jslint.com

Click the 'The Good Parts' button and tell him all of his JS has to go through that. Enjoy his tears!!!

EDIT: oh, plus this! Strict Mode :woop:

Lumpy fucked around with this message at 16:50 on Jan 5, 2011

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

geeves posted:

I admit I don't like the way it looks. Perhaps because I would never do that in Java. Especially if the returned object was more complex than just 3 lines. Is it interpreted differently than

code:
APP.Thing = (function () {
  var privateVar = 'whee',
         privateMethod = function () {
             //stuff;
         };
  var obj = {
      publicVar: "pants",
      publicMethod: function () {
        alert(privateVar);
  }
   return obj;
  }
}());
? Again, maybe this is more personal preference. But back to your original point - I kinda wish JavaScript would strictly enforce the semi-colon.

Functionally they are similar, but in your case you are creating a private, retained var 'obj' when you don't need to. That said, creating a private object and returning it *is* useful for Functional Inheritance:

code:
var APP = {};
APP.person = function (stuff) {
    var me = stuff || {};
    // crappy example way of setting defaults
    me.name = stuff.name || "Some Goon";
    me.age = stuff.age || 99;

    me.sayName = function () {
        return "Hey, my name is " + me.name;
    };
    me.sayAge = function () {
        return "I am " + me.age + " years old!";
    };
    return me;
};

APP.internetGirl = function (stuff) {
    var me = APP.person(stuff);

    me.sayAge = function () {
        if (me.age < 18) {
            return "Get away you pedo!";
        } else {
            return "Yes, I'm legal!";
        }
    };
    return me;
};

var a = APP.internetGirl({age: 17.9});
a.sayAge(); // :pedo:
EDIT Code is now JSlinted! :v:

Lumpy fucked around with this message at 21:08 on Jan 5, 2011

NotShadowStar
Sep 20, 2000

Lumpy posted:

Functionally they are similar, but in your case you are creating a private, retained var 'obj' when you don't need to. That said, creating a private object and returning it *is* useful for Functional Inheritance:

code:
APP.person = function (stuff) {
 var me = stuff || {};
 // crappy example way of setting defaults
 me.name = stuff.name || "Some Goon";
 me.age = stuff.age || 99;

 me.sayName = function () {
   return "Hey, my name is " + me.name;
 };
 me.sayAge = function () {
   return "I am " + me.age + " years old!";
 }
 return me;
}

APP.internetGirl = function (stuff) {
  var me = APP.person(stuff);

  me.sayAge = function () {
   if (me.age < 18) {
    return "Get away you pedo!";
   } else {
    return "Yes, I'm legal!";
   }
  }
  return me;
};

var a = APP.internetGirl({age: 17.9});
a.sayAge(); // :pedo:

Weren't you just going on about auto-inserted semi-colons? :v:

I miss semi-colons all the time in var f = function () {}; as well though. It's unlike any other language so it's easy to forget.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

NotShadowStar posted:

Weren't you just going on about auto-inserted semi-colons? :v:

I miss semi-colons all the time in var f = function () {}; as well though. It's unlike any other language so it's easy to forget.

Hahah.. that's what I get for not JSLinting my forum posts. :D

NotShadowStar
Sep 20, 2000
That reminds me, is there some sort of command-line JSLint like YUICompressor? I use the Rhino-based YUI compressor as a part of a build system and it's really handy. It looks like there used to be but it doesn't exist anymore.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

NotShadowStar posted:

That reminds me, is there some sort of command-line JSLint like YUICompressor? I use the Rhino-based YUI compressor as a part of a build system and it's really handy. It looks like there used to be but it doesn't exist anymore.

There is: http://www.javascriptlint.com/

Make sure you check out the config file and set your options accordingly. I have this installed and integrated into MacVim, and it's super-duper awesome.

EDIT: if you are on a Mac and have homebrew installed, you can install it with: brew install jsl

EDIT THE SECOND: it also doesn't do the whitespace checks that Corkford's "Good Parts" will.

Lumpy fucked around with this message at 22:09 on Jan 5, 2011

NateTheGreat
Jul 6, 2010
This is really bugging me, I'm trying to access trends from a JSON on Twitter's API but it seems like the array that holds them is named as a date...which starts with a number. Obviously when I try to use it I get a syntax error, what's the deal? Here's the JSON: http://api.twitter.com/1/trends/weekly.json

Tres Burritos
Sep 3, 2009

Alright, so I got basic animations working using jquery, .animate() is pretty cool. So my next question is, what is the best way to get textual data and then put it up on my webpage?

To elaborate on this question, here's my situation:
I've got a navigation bar with links on the left side of the page, when you click on one of the navigation links it does the nice little animation (thanks to Nigglypuff and Lumpy). The plan is to load some text into the "centerDoc" in between the fade in and fade out animations. Should I put some .txt files somewhere on my domain and grab them? or put the text right in the mainpage html? Similarly, once I've gotten the text I've got to size the height of the "centerDoc". What I'm thinking of doing is getting the number of characters in the text and then doing some math and see how many lines the whole thing is, and then make that the height. Is that stupid? Is there a better way to do this?

edit: I'm anticipating the content to change frequently enough to make the height sizing thing worth my time

Also, would it be a faux pas to put a link to the page so you can rip me a new one?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

NateTheGreat posted:

This is really bugging me, I'm trying to access trends from a JSON on Twitter's API but it seems like the array that holds them is named as a date...which starts with a number. Obviously when I try to use it I get a syntax error, what's the deal? Here's the JSON: http://api.twitter.com/1/trends/weekly.json

code:
var a = {"pants": {"99": ["a","b","c"]}};
alert(a.pants['99'][1]); // "b"

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Tres Burritos posted:

Alright, so I got basic animations working using jquery, .animate() is pretty cool. So my next question is, what is the best way to get textual data and then put it up on my webpage?

To elaborate on this question, here's my situation:
I've got a navigation bar with links on the left side of the page, when you click on one of the navigation links it does the nice little animation (thanks to Nigglypuff and Lumpy). The plan is to load some text into the "centerDoc" in between the fade in and fade out animations. Should I put some .txt files somewhere on my domain and grab them? or put the text right in the mainpage html? Similarly, once I've gotten the text I've got to size the height of the "centerDoc". What I'm thinking of doing is getting the number of characters in the text and then doing some math and see how many lines the whole thing is, and then make that the height. Is that stupid? Is there a better way to do this?

edit: I'm anticipating the content to change frequently enough to make the height sizing thing worth my time

Also, would it be a faux pas to put a link to the page so you can rip me a new one?

If the text is going to change a lot, keep it in separate files. Why do you need to count characters? The container will resize itself unless you explicitly set it's height. To get the new content in:

jQuery('#someDiv').load('someFile.html');

EDIT: and yes, post a link!

EDIT^2: the docs on jQuery.load() http://api.jquery.com/load/

Lumpy fucked around with this message at 05:09 on Jan 7, 2011

Tres Burritos
Sep 3, 2009

So I am explicitly telling the container it's height when I animate it, 'cause I like the way the animation looks. I'm not sure if there's some CSS property that I could be using like "toggle", so as far as I know, counting is my only option.

Have a look at my lovely webpage!
https://www.somethinglikecorkscrews.com

Tres Burritos fucked around with this message at 05:28 on Jan 7, 2011

NateTheGreat
Jul 6, 2010

Lumpy posted:

code:
var a = {"pants": {"99": ["a","b","c"]}};
alert(a.pants['99'][1]); // "b"

Oh wow I had not thought of that, thanks!

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Tres Burritos posted:

So I am explicitly telling the container it's height when I animate it, 'cause I like the way the animation looks. I'm not sure if there's some CSS property that I could be using like "toggle", so as far as I know, counting is my only option.

Have a look at my lovely webpage!
https://www.somethinglikecorkscrews.com

First off, don't take this as ripping you a new one :) As someone who is completely self-taught, I learned everything I know about web development from people telling me all the things I did wrong, and boy howdy, there was lots of it!

1. If you have a block level element that only has one block level child, you must punch yourself in the crotch as hard as you can then remove it. (This is know as "Lumpy's Law" in the Web Design foums.)
code:
<ul id="nav">
  <li> ... </li>
</ul>
[/code[

is functionally the same as
[code]
<div id="nav">
  <ul>
    <li> ... </li>
  </ul>
</div>
Anything you are doing to that DIV will work just the same on the UL.

2. Gratuitous Animation. Don't do it. Yes, absolutely create cool animated effects for fun, but don't use them unless the enhance the user experience. If I visit your page and I want to take a look at your resume or projects, I have to wait a minimum of 3 seconds (assuming your content is already on the page, not loaded via ajax.) A 3 second page load is horrible under normal circumstances, but you are ensuring the user never has one LESS than that. Three seconds seems short when you say it, but get out a stopwatch and surf for a bit. Time pages, especially sites you say "drat, this is taking FOREVER!!!" That slow site still loaded in under three seconds I bet.

If the purpose of the page is to showcase that effect, then sure, it's appropriate, but if not, get rid of it. There are only a small set of use cases where animation is appropriate.

3. Don't use the HTML 4.01 Transitional Doctype. Use HTML5 or if you have a really, really, really good reason (you probably don't) use XHMTL 1.0 Strict. HTML 4.01 Transitional is from 1999... unless you really are going to use FONT tags and need to validate. :D

4. jQuery's animate() can animate based on percentages, so you don't need to pixel calculate at all on resizing back up.

dark_panda
Oct 25, 2004

NotShadowStar posted:

That reminds me, is there some sort of command-line JSLint like YUICompressor? I use the Rhino-based YUI compressor as a part of a build system and it's really handy. It looks like there used to be but it doesn't exist anymore.

The code for the Rhino script is strangely missing from the jslint site, but here's what you do:

0. Install Rhino through whatever means you'd normally use.

1. Get the jslint script:

https://github.com/douglascrockford/JSLint (you'll want fulljslint.js)

2. Get the Rhino script:

http://trac.jwchat.org/jsjac/browser/utils/rhino.js (just a random site I found it on, but it used to be at http://www.jslint.com/rhino/index.html)

3. Append the Rhino script to the fulljslint.js script and put a bang into the first line of the script along the lines of

code:
#!/usr/bin/env rhino
4. Stick the script into your path and make it executable.

Now you can go to town. I name my copy jslint myself, and you can just run "jslint <whatever file>" now. Options have to be set in the file itself (see the Rhino script and the boolean switches and stuff). Enjoy.

Tivac
Feb 18, 2003

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

NotShadowStar posted:

That reminds me, is there some sort of command-line JSLint like YUICompressor? I use the Rhino-based YUI compressor as a part of a build system and it's really handy. It looks like there used to be but it doesn't exist anymore.

For another option, I wrote about how to do this using the Windows Scripting Host a while ago, the post is about running it as an editor tool but most of it is still applicable.

http://patcavit.com/2010/05/11/jslint-in-programmers-notepad-revisited/

Insurrectum
Nov 1, 2005

I'm trying to write a greasemonkey script that finds and replaces text on a facebook page. I don't have experience with javascript but I do have programming experience and have managed to get the script loading on each new pageload, and now I need to get the functionality in place. I assume it involves the "string.replace(reg/exp,"replaced string")" or something like that, but I don't know how to get from the document class to the strings making up the html.

Masked Pumpkin
May 10, 2008
OK, it's been a long while since I last worked with javascript, and I'm tearing my hair out with what seems like it should be the simplest problem ever. A friend wants her page, which makes extensive use of framesets, to dynamically resize based on screen resolution. No problem, I thought, and quickly tapped out the following code:

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<title>Main Page</title>
		<link href="../css/basic.css" type="text/css" rel="stylesheet" media="all" />
	</head>
	<frameset rows="237,*" cols="*" border="0" frameborder="no" framespacing="0">
		<script type="text/javascript">
			int ScreenWidth = screen.width;
			int FirstCol = ((ScreenWidth/100)*15);
			int SecondCol = ((ScreenWidth/100)*70);
			int ThirdCol = ((ScreenWidth/100)*15);
			if (SecondCol < 716) {
				SecondCol = 716;
			}
			document.write('<frameset cols="*,'+ SecondCol + ',*">');
		</script>
			<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />
			<frame name="topFrame" src="header.html" scrolling="no" />
			<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />
		</frameset>
The result is... disappointing - it appears as if the document.write is breaking things and is not being put through properly, and so the columns are completely out. I'd just use frameset percentages, but the next block of html suffers from a rounding error in Chrome and winds up 2 pixels out on most resolutions (it uses percentages of 15, 13, 57 and 13) and so drops out. I'm trusting javascript can manage the rounding nicely, but in any case if I can get a proper value for the far left or right columns, I can apply that to both blocks of framesets.

It's bugging the hell out of me, it should be the simplest thing but I've googled endlessly and am at a complete loss. Where should I be looking?

dark_panda
Oct 25, 2004

Masked Pumpkin posted:

code:
	int ScreenWidth = screen.width;
	int FirstCol = ((ScreenWidth/100)*15);
	int SecondCol = ((ScreenWidth/100)*70);
	int ThirdCol = ((ScreenWidth/100)*15);

There is no "int" keyword in JavaScript. You should be getting a syntax error, no?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Insurrectum posted:

I'm trying to write a greasemonkey script that finds and replaces text on a facebook page. I don't have experience with javascript but I do have programming experience and have managed to get the script loading on each new pageload, and now I need to get the functionality in place. I assume it involves the "string.replace(reg/exp,"replaced string")" or something like that, but I don't know how to get from the document class to the strings making up the html.

"It depends" :D

To access the DOM, there are a bunch of different methods you can use. For example, if you are looking for an element witn a specific ID:

code:
var myEl = document.getElementById("someId");
// do stuff to myEl
Like I said, there are a lot of methods for finding, creating, removing, etc. nodes. Google "javascript DOM methods" or something similar.

Insurrectum
Nov 1, 2005

Lumpy posted:

"It depends" :D

To access the DOM, there are a bunch of different methods you can use. For example, if you are looking for an element witn a specific ID:

code:
var myEl = document.getElementById("someId");
// do stuff to myEl
Like I said, there are a lot of methods for finding, creating, removing, etc. nodes. Google "javascript DOM methods" or something similar.

Thanks! I managed to figure it out.

Masked Pumpkin
May 10, 2008

dark_panda posted:

There is no "int" keyword in JavaScript. You should be getting a syntax error, no?

If only I were - the weird thing I'm running into, after I gave up on pasting the code together and used a bunch of if...else statements to resize, I'm still not getting anything, although after moving the <body> tags around I'm at least getting some progress - writing out screen.width is giving the appropriate response, but none of the if statements are triggering, before and after I put quotes around the numbers to check.

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<title>Main Page</title>
		<link href="../css/basic.css" type="text/css" rel="stylesheet" media="all" />
	</head>
<body>
	<frameset rows="237,*" cols="*" border="0" frameborder="no" framespacing="0">
		<script type="text/javascript">
			var ScreenWidth = screen.width;
			//document.write(ScreenWidth);
			if (ScreenWidth <= '1024') {
				document.write('<frameset cols="*,716,*">');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('<frame name="topFrame" src="header.html" scrolling="no" />');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('</frameset>');
				document.write('<frameset cols="*,133,583,*">');
			}
			else if (ScreenWidth <= '1280') {
				document.write('<frameset cols="*,896,*">');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('<frame name="topFrame" src="header.html" scrolling="no" />');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('</frameset>');
				document.write('<frameset cols="*,166,730,*">');
			}
			else if (ScreenWidth <= '1440') {
				document.write('<frameset cols="*,1008,*">');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('<frame name="topFrame" src="header.html" scrolling="no" />');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('</frameset>');
				document.write('<frameset cols="*,187,821,*">');
			}
			else if  (ScreenWidth > '1441') {
				document.write('<frameset cols="*,1344,*">');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('<frame name="topFrame" src="header.html" scrolling="no" />');
				document.write('<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />');
				document.write('</frameset>');
				document.write('<frameset cols="*,249,1095,*">');
			}
		</script>
			<frame name="leftFrame" src="black-bg.html" noresize="noresize" scrolling="no" />
			<frame src="../images/InSite%20Web%20Left%20Info%20Banner.swf" name="NoName" />
			<frame name="mainFrame" src="home.html" />
			<frame src="black-bg.html" name="NoName" noresize="noresize" />
		</frameset>
		<noframes>
			</body>
		</noframes>
	</frameset>
</html>
The whole thing runs without generating a single syntax error.

Nigglypuff
Nov 9, 2006


BUY ME BONESTORM
OR
GO TO HELL
I don't think <script> tags inside a <frameset> are evaluated at all.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
document.write also isn't supported in xhtml, although I suppose you're obviously serving the page as html as it isn't well-formed xml either.

Insurrectum
Nov 1, 2005

What does writing a firefox extension entail outside of using Javascript? I want to make an extension that accesses a file on my harddrive and changes facebook profiles in a certain way for certain people and keeps track of some data, but greasemonkey can't access local files so it looks like I'm stuck writing an actual extension. I see I'll have to use something called XUL, is that going to be a major hurdle or just something that can be picked up in an afternoon or two? I have a moderate amount of programming experience.

fishmech
Jul 16, 2006

by VideoGames
Salad Prong
I figure this is the right place to ask:

I've been using this user script for a couple of years:
code:
// ==UserScript==
// @name           New Posts in New Tabs
// @description    Adds a button to open all threads with new posts in new tabs.
// @include        http://forums.somethingawful.com/*
// @author         RedKazan
// ==/UserScript==

var forum = document.getElementById("forum");
if (forum)
{	
	var button = document.createElement("a");
	button.href = "#";
	button.innerHTML = "Open New Posts in New Tabs";
	button.style.cssFloat = "right";
	button.style.marginRight = "8px";
	button.addEventListener("click", NewPostsInNewTabs, false);

	var where = document.evaluate("THEAD/TR/TH[contains(@class,'title')]",
			forum, null, 7, null);
	where = where.snapshotItem(0);
	if (where)
	{
		where.insertBefore(button,where.firstChild);
	}
}

function NewPostsInNewTabs(event)
{
	var eval, node, name;
	event.preventDefault();
	eval = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			document.getElementById("forum"), null, 7, null);
	for (i = 0; i < eval.snapshotLength; i++)
	{
		node = eval.snapshotItem(i);
		name = node.parentNode.nextSibling.nextSibling.childNodes[1].innerHTML;
		GM_openInTab(node.href,name);
	}
	
	return;
}
As the title says it's supposed to open all threads with new posts on the page in new tabs.
In Firefox 3.6.x it works fine whether in Greasemonkey or compiled to an XPI. In Firefox 4 Greasemonkey doesn't work at all, and if it's compiled in an xpi this script doesn't work past Beta 3. So I think there's generally some old bad code or references in there that should be changed.

Anyway it was working fine in Scriptish which is Greasemonkey that works on Firefox 4 up until Beta 8. Beta 9 came out today and now, instead of opening all the threads, it will only open the first thread it finds.

How can it be fixed to open all the threads again? Does it need to just have the "NewPostsInNewTabs" function rewritten entirely and if so can anyone do that for me? I can give a forum upgrade to whoever gets this fully functional if neccesary.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

fishmech posted:

I figure this is the right place to ask:

I've been using this user script for a couple of years:
code:
// ==UserScript==
// @name           New Posts in New Tabs
// @description    Adds a button to open all threads with new posts in new tabs.
// @include        [url]http://forums.somethingawful.com/*[/url]
// @author         RedKazan
// ==/UserScript==

var forum = document.getElementById("forum");
if (forum)
{	
	var button = document.createElement("a");
	button.href = "#";
	button.innerHTML = "Open New Posts in New Tabs";
	button.style.cssFloat = "right";
	button.style.marginRight = "8px";
	button.addEventListener("click", NewPostsInNewTabs, false);

	var where = document.evaluate("THEAD/TR/TH[contains(@class,'title')]",
			forum, null, 7, null);
	where = where.snapshotItem(0);
	if (where)
	{
		where.insertBefore(button,where.firstChild);
	}
}

function NewPostsInNewTabs(event)
{
	var eval, node, name;
	event.preventDefault();
	eval = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			document.getElementById("forum"), null, 7, null);
	for (i = 0; i < eval.snapshotLength; i++)
	{
		node = eval.snapshotItem(i);
		name = node.parentNode.nextSibling.nextSibling.childNodes[1].innerHTML;
		GM_openInTab(node.href,name);
	}
	
	return;
}
As the title says it's supposed to open all threads with new posts on the page in new tabs.
In Firefox 3.6.x it works fine whether in Greasemonkey or compiled to an XPI. In Firefox 4 Greasemonkey doesn't work at all, and if it's compiled in an xpi this script doesn't work past Beta 3. So I think there's generally some old bad code or references in there that should be changed.

Anyway it was working fine in Scriptish which is Greasemonkey that works on Firefox 4 up until Beta 8. Beta 9 came out today and now, instead of opening all the threads, it will only open the first thread it finds.

How can it be fixed to open all the threads again? Does it need to just have the "NewPostsInNewTabs" function rewritten entirely and if so can anyone do that for me? I can give a forum upgrade to whoever gets this fully functional if neccesary.

Could be that it's using a lot of reserved words as variable names.

EDIT: also, your i counter variable is global amongst other things; there's lots of mistakes in there.

Lumpy fucked around with this message at 01:38 on Jan 15, 2011

dark_panda
Oct 25, 2004

Masked Pumpkin posted:

If only I were - the weird thing I'm running into, after I gave up on pasting the code together and used a bunch of if...else statements to resize, I'm still not getting anything, although after moving the <body> tags around I'm at least getting some progress - writing out screen.width is giving the appropriate response, but none of the if statements are triggering, before and after I put quotes around the numbers to check.

The whole thing runs without generating a single syntax error.

Let's try simplifying it a bit.

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<title>Main Page</title>
	<link href="../css/basic.css" type="text/css" rel="stylesheet" media="all" />

	<script type="text/javascript">
	//<![CDATA[
	window.onload = function() {
		var cols1, cols2;
		if (screen.width <= 1024) {
			cols1 = "*,716,*";
			cols2 = "*,133,583,*";
		}
		else if (screen.width <= 1280) {
			cols1 = "*,896,*";
			cols2 = "*,166,730,*";
		}
		else if (screen.width <= 1440) {
			cols1 = "*,1008,*";
			cols2 = "*,187,821,*";
		}

		if (cols1 && cols2) {
			document.getElementById('frameset-1').cols = cols1;
			document.getElementById('frameset-2').cols = cols2;
		}
	}
	//]]>
	</script>
</head>

<frameset rows="237,*" cols="*">
	<frameset cols="*,1344,*" id="frameset-1">
		<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />
		<frame name="topFrame" src="header.html" scrolling="no" />
		<frame src="black-bg.html" name="NoName" noresize="noresize" scrolling="no" />
	</frameset>

	<frameset cols="*,249,1095,*" id="frameset-2">
		<frame name="leftFrame" src="black-bg.html" noresize="noresize" scrolling="no" />
		<frame src="../images/InSite%20Web%20Left%20Info%20Banner.swf" name="NoName" />
		<frame name="mainFrame" src="home.html" />
		<frame src="black-bg.html" name="NoName" noresize="noresize" />
	</frameset>

	<noframes>
		<body>
			no frames
		</body>
	</noframes>
</frameset>
</html>
That appears to work and is valid XHTML and doesn't require document.write.

fishmech
Jul 16, 2006

by VideoGames
Salad Prong

Lumpy posted:

Could be that it's using a lot of reserved words as variable names.

EDIT: also, your i counter variable is global amongst other things; there's lots of mistakes in there.

Well the thing is this just a script another goon wrote like 2 or more years back and I've been using since then. I don't really know javascript but I do know it looks all wrong, but it was working.

I've tried changing variable names to make things not conflict with existing names but i get the same results, here's what I have now:
code:
// ==UserScript==
// @name           New Posts in New Tabs
// @description    Adds a button to open all threads with new posts in new tabs.
// @include        http://forums.somethingawful.com/*
// @author         RedKazan
// ==/UserScript==

var forum = document.getElementById("forum");
if (forum)
{	
	var button = document.createElement("a");
	button.href = "#";
	button.innerHTML = "Open New Posts in New Tabs";
	button.style.cssFloat = "right";
	button.style.marginRight = "8px";
	button.addEventListener("click", NewPostsInNewTabs, false);

	var where = document.evaluate("THEAD/TR/TH[contains(@class,'title')]",
			forum, null, 7, null);
	where = where.snapshotItem(0);
	if (where)
	{
		where.insertBefore(button,where.firstChild);
	}
}

function NewPostsInNewTabs(event)
{
	var evil, nade, nome;
	event.preventDefault();
	evil = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			document.getElementById("forum"), null, 7, null);
	for (iddt = 0; iddt < evil.snapshotLength; iddt++)
	{
		nade = evil.snapshotItem(iddt);
		nome = nade.parentNode.nextSibling.nextSibling.childNodes[1].innerHTML;
		GM_openInTab(nade.href,nome);
	}
	
	return;
}
Again, if you or anyone else can fix this up for me or something, I can get you a forum upgrade.

NotShadowStar
Sep 20, 2000
Any better?
code:
// ==UserScript==
// @name           New Posts in New Tabs
// @description    Adds a button to open all threads with new posts in new tabs.
// @include        [url]http://forums.somethingawful.com/*[/url]
// @author         RedKazan
// ==/UserScript==

var postsInNewTabs = function (event) {
	var evil, nade, nome, i;
	event.preventDefault();
	evil = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			                      document.getElementById("forum"), null, 7, null);
	for (i = 0; i < evil.snapshotLength; i = i + 1) {
		nade = evil.snapshotItem(i);
		nome = nade.parentNode.nextSibling.nextSibling.childNodes[1].innerHTML;
		GM_openInTab(nade.href, nome);
	}
	return;
};

var forum = document.getElementById("forum");
if (forum) {	
	var button = document.createElement("a"),
      where;
	button.href = "#";
	button.innerHTML = "Open New Posts in New Tabs";
	button.style.cssFloat = "right";
	button.style.marginRight = "8px";
	button.addEventListener("click", postsInNewTabs, false);

	where = document.evaluate("THEAD/TR/TH[contains(@class,'title')]",
			forum, null, 7, null);
	where = where.snapshotItem(0);
	if (where) {
		where.insertBefore(button, where.firstChild);
	}
}

fishmech
Jul 16, 2006

by VideoGames
Salad Prong

NotShadowStar posted:

Any better?
code:
// ==UserScript==
// @name           New Posts in New Tabs
// @description    Adds a button to open all threads with new posts in new tabs.
// @include        http://forums.somethingawful.com/*
// @author         RedKazan
// ==/UserScript==

var postsInNewTabs = function (event) {
	var evil, nade, nome, i;
	event.preventDefault();
	evil = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			                      document.getElementById("forum"), null, 7, null);
	for (i = 0; i < evil.snapshotLength; i = i + 1) {
		nade = evil.snapshotItem(i);
		nome = nade.parentNode.nextSibling.nextSibling.childNodes[1].innerHTML;
		GM_openInTab(nade.href, nome);
	}
	return;
};

var forum = document.getElementById("forum");
if (forum) {	
	var button = document.createElement("a"),
      where;
	button.href = "#";
	button.innerHTML = "Open New Posts in New Tabs";
	button.style.cssFloat = "right";
	button.style.marginRight = "8px";
	button.addEventListener("click", postsInNewTabs, false);

	where = document.evaluate("THEAD/TR/TH[contains(@class,'title')]",
			forum, null, 7, null);
	where = where.snapshotItem(0);
	if (where) {
		where.insertBefore(button, where.firstChild);
	}
}

Shoot, it still only loads one thread. Maybe it's just a Firefox problem then.

NotShadowStar
Sep 20, 2000
I'm not a Firefox ninja, but the code that finds all of the unread threads and stuffs them into an array works as advertised. Perhaps spawning tabs is throttled in Firefox 4, which is probably a good thing.

fishmech
Jul 16, 2006

by VideoGames
Salad Prong

NotShadowStar posted:

I'm not a Firefox ninja, but the code that finds all of the unread threads and stuffs them into an array works as advertised. Perhaps spawning tabs is throttled in Firefox 4, which is probably a good thing.

It's just weird. It worked fine before this new beta came out. Would there be a way to make it wait or something in between spawning each new tab?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

fishmech posted:


I've tried changing variable names to make things not conflict with existing names but i get the same results, here's what I have now:

I know it' not your script, but there are still reserved words in there:

javascript jesus posted:

JavaScript is very heavy handed in its restrictions on reserved words. The reserved words are
code:
abstract
boolean break byte
case catch char class const continue
debugger default delete do double
else enum export extends
false final finally float for function
goto
if implements import in instanceof int interface
long
native new null
package private protected public
return
short static super switch synchronized
this throw throws transient true try typeof
var volatile void
while with
Most of those words are not even used in the language. A reserved word cannot be used

As a name in literal object notation
As a member name in dot notation
As a function argument
As a var
As an unqualified global variable
As a statement label

There is no excuse for the first two restrictions. None. There is an excuse for the second two restrictions, but it is very weak.


http://www.crockford.com/javascript/survey.html


as for the putting a delay in, yes (please note I am tired and this likely contains syntax errors):

code:
var postsInNewTabs = function (event) {
	var evil, nade, nome, myCount = 0,
              popTab = function () {
// I assume 'snapshotLength' is a built in property or something as it's never defined....
                if (myCount < evil.snapshotLength) {  
                   nade = evil.snapshotItem(myCount);
		   nome = nade.parentNode.nextSibling.nextSibling.childNodes[1].innerHTML;
		   GM_openInTab(nade.href, nome);
                   myCount += 1;
                   setTimeout(popTab, 250);
                };
	event.preventDefault();
	evil = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			                      document.getElementById("forum"), null, 7, null);
        popTab();
};

Lumpy fucked around with this message at 06:26 on Jan 15, 2011

fishmech
Jul 16, 2006

by VideoGames
Salad Prong
Ah cool. For what it's worth I got someone to hack up this version which works for now:
code:
// ==UserScript==
// @name           test
// @namespace      testing
// @include        http://forums.somethingawful.com/*
// ==/UserScript==

var forum = document.getElementById("forum");
if (forum)
{	
	var button = document.createElement("a");
	button.href = "#";
	button.innerHTML = "Open New Posts in New Tabs";
	button.style.cssFloat = "right";
	button.style.marginRight = "8px";
	button.addEventListener("click", NewPostsInNewTabs, false);

	var where = document.evaluate("THEAD/TR/TH[contains(@class,'title')]",
			forum, null, 7, null);
	where = where.snapshotItem(0);
	if (where)
	{
		where.insertBefore(button,where.firstChild);
	}
}

function NewPostsInNewTabs(event)
{
	var eval, node, name;
	event.preventDefault();
	eval = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
			document.getElementById("forum"), null, 7, null);
	OpenTabLoop(eval);

	return;
}

var i =0;
function OpenTabLoop(eval){
	if (i < eval.snapshotLength){
		setTimeout(function(){		
			node = eval.snapshotItem(i);
			name = node.parentNode.nextSibling.nextSibling.childNodes

[1].innerHTML;
			i++;
			OpenTabLoop(eval);
			GM_openInTab(node,name);
			
		},(i*3)+100);
	}

}

Masked Pumpkin
May 10, 2008

dark_panda posted:

Let's try simplifying it a bit.

code:
Code which works
That appears to work and is valid XHTML and doesn't require document.write.

You are a scholar and a gentleman, good sir - a thousand thanks!

Vanadium
Jan 8, 2005

I bet you guys add superfluous semicolons in lua or ruby too. :colbert:

Adbot
ADBOT LOVES YOU

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Vanadium posted:

I bet you guys add superfluous semicolons in lua or ruby too. :colbert:

Yeah, we should totally let the interpreter add them for us, even though there are cases where doing that will break your code! What exactly is a "superfluous" semicolon in javascript anyway?

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