CyberD.org
C:\ Home » Code » A Little Random Title Script

A Little Random Title Script

I made a little random title script for the movie review pages with more than one review. A little impulsive thing... that took 40 minutes to figure out. Ugh. Here's how it turned out though, in two parts. It's in JS.

First there's the header, that either shows a random title from a specific list, or if the user has JS disabled, a standard one:

<h1><script src="/theme/js/more-reviews.js" type="text/javascript"></script><noscript>More Reviews!</noscript></h1>

Then there's the script with the aforementioned list, and code to cycle through the titles randomly, above refereed to as more-reviews.js:

var r_text = new Array ();
r_text[0] = "More Reviews!";
r_text[1] = "Added Addages!";
r_text[2] = "Ongoing Opinions!";
r_text[3] = "Latter Ramblings!";
r_text[4] = "Future Views!";
r_text[5] = "Later Watches!";

var i = Math.floor(7*Math.random());

if (navigator.appName == "Netscape")
   {
   document.write(r_text[i]);
   }
else
   {
   document.write(r_text[i]);
   }

That's it! Depending on your number of titles you might need to change the formula below the list too. I tried using a solution that didn't rely on numbers at all first, but just couldn't get it working, so this is it.

This is what it looks like in action (reload page to see more):

It's such a simple thing, but I dug through such a huge mess of other methods before finding one that for some reason actually worked, that I felt like I needed to post about it. It's not all that strange this one did, but it's strange the others didn't. I still have no idea why. Something with the way H1 tags work? Something about including a JS script in a header? If you have any knowledge about such limitations I'd be intrigued to hear.

So, stranger who stumbled upon this post, if you're wise then feel free to send me a message, and if you're in the same situation - searching for a solution to the same problem, hopefully this will find you faster than it found me! And hopefully it'll work for you, too. It's just a little random random title script, but it sure took a big effort to get right.

Comments

Keep track of the discussion via rss? Read about comment etiquette? Or type in something below!
  1. S3C
    Sunday Dec/10/2017

    how do you run this and why bother create an if statement if both branches execute the same thing?

  2. Cyber
    Sunday Dec/10/2017

    You just embed it in any HTML page, wherever you want. Doesn't have to be in a header either. You could include the JS directly if you can't save it separately, it'd just get messy to have all that code in the middle of the page in my case. Makes it easier to edit when all instances refer to the same script, too. Better answer: https://www.quora.com/How-do-I-run-JavaScript-code

    If you ever want to just play around with code and test stuff right away: https://jsfiddle.net/

    As for the double statement hmm, man, I didn't notice that before. It's just a bit I've been using for all these things. It's possible it's some weird compatibility thing... will need to research!

  3. S3C
    Tuesday Dec/19/2017

    I couldn't get that site to compile, so I used http://www.js.do instead.

    any idea of how to make JS extensions?

  4. Cyber
    Tuesday Dec/19/2017

    Oh cool, a cool site I didn't know about!

    Yupp, been using Userscripts for a while, with bits of JS just for specific sites. Good guide/script directory here: https://greasyfork.org/

    Basically you'll need to install a browser add-on to load specific scripts, and then just install/make the bits you need. They'll load either for all or just specific intended sites, and can easily be toggled on/off. There's been a bunch of them made for NG over the years, though badly maintained now: https://knugen.newgrounds.com/news/post/444342

    ...think I have the links to at least one other similar lists somewhere if you're interested.

    You can always run a JS directly on a particular page via the browser's developer console too, btw, but it'll be a manual thing each time. Shortcut keys for that vary with the browser, SHIFT + F12 on Chrome, I think. On FF I usually just right-click and 'inspect element' to open that up. Great for taking a full-page screenshot too, outside the visible area.

  5. S3C
    Tuesday Dec/19/2017

    ahh, cool, good thinking...I will try the console option out.

    any idea how I could grab variables from another script? For example, say I wanted to grab the r_text variable from more-reviews.js.

    My example script here would first grab that variable. If r_text isn't equal to Added Adages! then it will refresh the page. The script would continue to execute until r_text = Added Adages!

  6. S3C
    Tuesday Dec/19/2017

    nvm...found what I was looking for:

    document.getElementsByClassName(object name)[0].someAction();

    not sure why the index is necessary, but it works. try it:

    document.getElementsByClassName("btn")[0].click();

    also, on FireFox, there's actually a built-in javascript editor, press shift + F4

  7. S3C
    Tuesday Dec/19/2017

    ok, so I know why there commands needs an index. you can technically have different instances of a class on your page, hence they are indexed in an array...(I said 'object name', it should be class. 'btn' is the name of the class for your button here.)

  8. Cyber
    Tuesday Dec/19/2017

    Wonder what you're trying to make there hmmm...

    Oh cool the Randomly Wise prompt via alternate method. XD Learning quite a bit here. Wonder how long you've been fiddling around with JS? New hobby? I'm still mostly molding together the scripts I use via bits and pieces I find elsewhere, don't yet fully understand how everything I use works.

    Thanks for the note on that Scratchpad too! Notepad built into the browser! :D Wonder if Chrome has anything similar...

  9. Cyber
    Tuesday Dec/19/2017

    The console shortcut there was just plain F12 btw, no need to shift.

  10. S3C
    Wednesday Dec/20/2017

    not much, just visiting those websites where content is not indexed by pages, but rather seamlessly loaded on the same page by clicking a button (or scrolling). It would be impossible to download all this content via automation, unless I was able to execute a script that automatically clicks such buttons so everything can be loaded within the html.

    I suppose I started yesterday. There's much to learn about languages. But you don't always need to understand a programming language to complete a task...just the logic and paradigms, which are universal.

  11. Cyber
    Wednesday Dec/20/2017

    Aha! I like where this is going. :) Such a script would be interesting to try out oncet ready!

    For sure. Sounds like you have a natural talent for this type of logic, though, I've been around code so long but still mastered little outside the details that deal with design and designation.

  12. S3C
    Wednesday Dec/20/2017

    I suppose it is ready, except it ooesn't actually load to the html file, just the browser. So if you try to view the source or save the page, it will only have the content before you press any buttons...Obviously it's still accessible somewhere, just not sure where...

  13. Cyber
    Thursday Dec/21/2017

    Yeah I've been wondering how they actually load in that content too. Definitely not via a static script, since that'd defeat the purpose of a 'load more' feature in the first place... this thing could currently load up an endless array in one swoop though? Like NG feed history?

  14. S3C
    Thursday Dec/21/2017

    yes, in one swoop.

  15. S3C
    Thursday Dec/21/2017

    so...actually all you have to do see the generated content is select all + view source.

  16. Cyber
    Thursday Dec/21/2017

    Awesome. :) After it´s been loaded right? Possible to send over that script via PM, if you don't feel like posting?

  17. S3C
    Friday Dec/22/2017

    var x = 0, amount = 1000;
    function openContent() {
    document.getElementsByClassName("button")[0].click();
    x++;
    if( x 1000 times, or until all the content is loaded. Using these two metrics (1000 clicks with a 1000 ms delay) it works for about ~250 clicks. of course you can set x higher. you could try to set the ms parameter in setTimeout() lower too, but it's really a matter of how fast the browser can load the new content once the button is clicked.

    this is just a brute force method, but it works. I though a simple loop would work for this, but the execution is too fast. IOW, the script doesn't wait for the browser to actually register a click, it just executes the JS command and proceeds regardless. For example, I had x set to 100 in a loop, but only got one click command before the loop was the finished. I could alternatively set x to Javascript's max integer size, but Firefox wouldn't like that. so that's why you have to use setTimeout() between clicks.

    i also find it weird how setTimeout() works, you can't just enter the time as a parameter, you have to pass it a function as well. So essentially what is happening here is the last line (OpenContent()) executes first, and the function is called recursively (in a sense) as long as x > 1000.

  18. S3C
    Friday Dec/22/2017

    oops accidentally cut off the code.

    var x = 0, amount = 1000;
    function openContent() {
    document.getElementsByClassName("button")[0].click();
    x++;
    if(x < amount){
    setTimeout(openContent(), 1000);
    }
    }
    openContent();

  19. S3C
    Friday Dec/22/2017

    also seems like the comment boxes don't
    like
    tabs

  20. S3C
    Friday Dec/22/2017

    but
    do
    multiple
    spaces
    work

  21. S3C
    Friday Dec/22/2017

    i guess not...btw ur getting spammed. time to cleanup

  22. Cyber
    Friday Dec/22/2017

    Yupp, tabs don't work! Multiple spaces           don't either. :P

    Have to have HTML like that sealed off to keep out possibly malicious spam comment stuff, but allowing for tabs and spaces hmm, that might be doable... further research required. Could turn into one of those tiny feature = 40 minute type of things. XD

    As for the script: looks good! Shall try it for the aforementioned feed in a bit.

  23. Cyber
    Friday Dec/22/2017

    Oh whoops, looks like HTML like that is NOT sealed off... looks like I should probably look up some stuff there too...



The Comment Form

Your email address will not be published. Required fields are marked *

Your email is saved only to approve your future comments automatically (assuming you really are a human). ;) It's not visible or shared with anyone. You can read about how we handle your info here.

Question   Smile  Sad   Redface  Biggrin  Surprised   Eek  Confused  Beardguy  Baka  Cool  Mad   Twisted  Rolleyes   Wink  Coin

Privacy   Copyright   Sitemap   Statistics   RSS Feed   Valid XHTML   Valid CSS   Standards

© CyberD.org 2025
Keeping the world since 2004.