Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Discussion on Boards.ie user scripts

  • 30-05-2012 5:17pm
    #1
    Closed Accounts Posts: 14,762 ✭✭✭✭


    28064212 I'm just having a look at the script for the spam report link, and I'm wondering could you give me a line-by-line breakdown of what's happening in the code? If it's not too much trouble. :)

    I'd be interested in trying to write some of this stuff myself, but there's a number of things in here that I don't get.
    function getParameterByName(name)
    {
    	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    	var regexS = "[\\?&]" + name + "=([^&#]*)";
    	var regex = new RegExp(regexS);
    	var results = regex.exec(window.location.search);
    	if(results == null)
    		return "";
    	else
    		return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    var loc = window.location.toString();
    if(loc.indexOf("showthread.php") != -1)
    {
    	var users = document.getElementsByClassName("bigusername");
    	for(var i = 0; i < users.length; i++)
    	{
    		var tr1 = users[i].parentNode.parentNode.parentNode;
    		var tr2 = tr1.nextSibling;
    		while(tr2 && tr2.nodeType == 3) // skips text nodes (e.g. whitespace)
    	        tr2 = tr2.nextSibling;
    		var td = tr2.firstChild;
    		while(td && td.nodeType == 3) // skips text nodes (e.g. whitespace)
    	        td = td.nextSibling;
    		var link = document.createElement("a");
    		link.href = "http://www.boards.ie/vbulletin/newreply.php?do=newreply&t=2056491247&customspamlink=" + users[i].href;
    		link.innerHTML = "Spammer";
    		td.appendChild(link);
    	}
    }
    else
    {
    	document.getElementById("vB_Editor_001_textarea").value = getParameterByName("customspamlink");
    	document.getElementsByName("emailupdate")[0].value = MYCHOICE;
    	document.getElementById("vB_Editor_001_textarea").focus();
    }
    

    Thanks a million. ;)


Comments

  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    28064212 I'm just having a look at the script for the spam report link, and I'm wondering could you give me a line-by-line breakdown of what's happening in the code? If it's not too much trouble. :)

    I'd be interested in trying to write some of this stuff myself, but there's a number of things in here that I don't get.

    Thanks a million. ;)
    Sure, no problem :). I'm assuming you do know some Javascript? Let me know if anything isn't clear.

    If you're not already using it, download the Firebug extension, it's very handy for inspecting the HTML of a page
    function getParameterByName(name)
    {
    /* You can pretty much ignore how this function works, just need to know what it does.
    It reads the current URL and returns the value of the given parameter.
    If the URL is http://www.test.com/?parama=v1&paramb=v2,
    getParameterByName("parama") will return v1, and getParameterByName("paramb") will return v2 */
    }
    var loc = window.location.toString();
    /*This gets the current URL. Because the script runs on two different pages, it
    needs to check which one it's on (which the if statement does) */
    if(loc.indexOf("showthread.php") != -1)
    {// In this branch, we're on a page which displays a thread
    	// This gets an array of all the bigusername elements (username displayed beside a post, above the avatar)
    	var users = document.getElementsByClassName("bigusername");
    	for(var i = 0; i < users.length; i++)
    	{
    		//I use tr1 and tr2 to get the row for each post that contains the "report post" button
    		//It's always in the same place relative to the bigusername element
    		var tr1 = users[i].parentNode.parentNode.parentNode;
    		var tr2 = tr1.nextSibling;
    		// These while loops just skip text-only elements. I want the first non-text element
    		while(tr2 && tr2.nodeType == 3)
    	        tr2 = tr2.nextSibling;
    		var td = tr2.firstChild;
    		while(td && td.nodeType == 3)
    	        td = td.nextSibling;
    		var link = document.createElement("a"); // create a link element
    		// set the target to the newreply page of the spammer notification thread, adding the
    		// customspamlink parameter and the users profile url
    		link.href = "http://www.boards.ie/vbulletin/newreply.php?do=newreply&t=2056491247&customspamlink=" + users[i].href;
    		// Set the text of the link to Spammer
    		link.innerHTML = "Spammer";
    		// Place the link after the report post button
    		td.appendChild(link);
    	}
    }
    else
    {// If we're not on showthread, we're on the reply page (script only runs on two different pages)
    	// Get the value of the customspamlink parameter
    	document.getElementById("vB_Editor_001_textarea").value = getParameterByName("customspamlink");
    	// Set the subscription method to the choice defined above ("Do Not Follow" by default)
    	document.getElementsByName("emailupdate")[0].value = MYCHOICE;
    	// Move the cursor to the 
    	document.getElementById("vB_Editor_001_textarea").focus();
    }
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    28064212 wrote: »
    Sure, no problem :). I'm assuming you do know some Javascript? Let me know if anything isn't clear.

    If you're not already using it, download the Firebug extension, it's very handy for inspecting the HTML of a page
    	// This gets an array of all the bigusername elements (username displayed beside a post, above the avatar)
    	var users = document.getElementsByClassName("bigusername");
    	for(var i = 0; i < users.length; i++)
    	{
    		//I use tr1 and tr2 to get the row for each post that contains the "report post" button
    		//It's always in the same place relative to the bigusername element
    		var tr1 = users[i].parentNode.parentNode.parentNode;
    		var tr2 = tr1.nextSibling;
    		// These while loops just skip text-only elements. I want the first non-text element
    		while(tr2 && tr2.nodeType == 3)
    	        tr2 = tr2.nextSibling;
    		var td = tr2.firstChild;
    		while(td && td.nodeType == 3)
    	        td = td.nextSibling;
    

    Yeah I'm ok with Javascript, though a bit rusty. I'm only really having problems with the array here. I mean I know in theory you should be getting at each post on the page... but then doesn't that mean that 'users' is an array of the user objects on the page? and for each of these objects you can get at that users memberID etc needed for the reporting spam link? 'bigusername' is the class name that's on each user on the page, right?


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Its an array of all elements on the page which have that CSS class. In this case those elements all contain hyperlinks to user profiles, from which you can extract their user id.

    They're not user objects as such.


  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    ah yeah I get ya. it's getting the memberID URL from the username link. I see it now. cool, that's great. all I need now is a good idea of what to have a go at :) thanks guys


  • Closed Accounts Posts: 7,872 ✭✭✭strobe


    I was trying to use the Beta skin before, and stopped using it when the hover over menu drove me crazy, appearing whenever I went up to a tab and had hovered for a brief second. So I went back to it to have a look at the code, to see if I could maybe write a script to delay the display of menu items on the hover, and discovered there is a way to change this with Zero Delay.

    So I wanted to look at this to see how it's done. I can see that there's a function called populatePrefs() and there's a div with id of prefmenu, which has another div inside with id of prefmenucontents. and that's where I get lost. Obviously I would need to get at the items that are shown in div 'prefmenucontents' but is this a matter of getting at the elements, as was above? or is there some more code on the page I should be looking at for this?

    Oh and should I start a thread in Development for this, instead of getting into the specifics of it here?


  • Advertisement
  • Closed Accounts Posts: 3,609 ✭✭✭Boards.ie: Danny


    Discussion split & moved from Site Development, it's become more a Q&A on general JS development :)


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    Thanks Danny. For anyone interested, the original thread with Greasemonkey scripts for Boards is here.
    strobe wrote: »
    I was trying to use the Beta skin before, and stopped using it when the hover over menu drove me crazy, appearing whenever I went up to a tab and had hovered for a brief second. So I went back to it to have a look at the code, to see if I could maybe write a script to delay the display of menu items on the hover, and discovered there is a way to change this with Zero Delay.

    So I wanted to look at this to see how it's done. I can see that there's a function called populatePrefs() and there's a div with id of prefmenu, which has another div inside with id of prefmenucontents. and that's where I get lost. Obviously I would need to get at the items that are shown in div 'prefmenucontents' but is this a matter of getting at the elements, as was above? or is there some more code on the page I should be looking at for this?
    Changing pre-existing functionality on the page in Greasemonkey is tricky. As far as I can see, the beta menu functions are in cascade.js. It's obfuscated, but you can use this to get readable code: http://jsbeautifier.org/.

    There is a way to do it, but it requires using Greasemonkey's "unsafeWindow", which, as the name indicates, isn't very safe, so I wouldn't really recommend it. That said, here's the code for the script (include it on all boards.ie pages):
    unsafeWindow.$(document).ready(function(){
    	var i = {
    		interval: 500,
    		sensitivity: 2,
    		over: unsafeWindow.addCat,
    		timeout: 400,
    		out: unsafeWindow.removeCat
    	};
    	unsafeWindow.$(".navitem").hoverIntent(i);
    });
    
    The key parameter is interval. This is the number of milliseconds before the menu is displayed. By default, it's only 100, this makes it 500 (half-second).

    unsafeWindow provides Greasemonkey with access to functions and variables which already exist on the page (e.g. the addCat function in cascade.js). However, it breaks the restricted security model of Greasemonkey. Userscripts run with a higher security access than webpage scripts, but if a userscript calls something within the webpage script, the webpage script now has more access than it should.

    So if, for examply, Boards.ie's Danny knew you were using it, they could edit the function you're calling (addCat) to do something malicious. There may be other exploits that I'm not aware of, so don't assume Danny's good nature is the only thing you have to rely on.

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    hmmm well you wouldn't want to be going about it like that then. but could you make up your own javascript that would override that function? without this unsafe version..?

    but in general you're saying that if there's already a function there, you can't/shouldn't be going trying to do something with it? so the point would be just to add new functionality.

    since the CSS is in the the source code, I assume that can be changed easier?

    sorry for all the questions :o


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    hmmm well you wouldn't want to be going about it like that then. but could you make up your own javascript that would override that function? without this unsafe version..?
    True, didn't think of that. You could get all the sub-elements of each navigation header and stick them into your own version of the menu, then override the hover function of each navigation item to show your menu instead of the default. Certainly code-able, although might be a pain to do it.
    but in general you're saying that if there's already a function there, you can't/shouldn't be going trying to do something with it? so the point would be just to add new functionality.
    As you point out above, you can just override the function. The advantage of the "unsafe" approach is it's just modifying the existing function slightly. The whole code's only 10 lines long. Using the override method means writing your own version of boards' menu code
    since the CSS is in the the source code, I assume that can be changed easier?
    Not sure what you mean here?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    28064212 wrote: »
    True, didn't think of that. You could get all the sub-elements of each navigation header and stick them into your own version of the menu, then override the hover function of each navigation item to show your menu instead of the default. Certainly code-able, although might be a pain to do it.

    No sorry I meant override the if for zerodelay of populatePrefs() with a function on the main page.

    28064212 wrote: »
    Not sure what you mean here?

    I mean the CSS can be changed easy here with script, yes? it's not as big a deal because it's on the same page as the source code.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    No sorry I meant override the if for zerodelay of populatePrefs() with a function on the main page.
    Ah ok, well then you just gave me the alternative idea by accident :)

    You wouldn't be able to override it without using unsafeWindow. You can't access a function or variable in a different script from a Greasemonkey one (unless you use unsafeWindow). The value would have to be held somewhere in the DOM. For example, if the checkbox "Zero Delay" had a value of "0" which was read by the cascade script, you could use the GM script to change that value to something else.

    However, the cascade script just checks whether "Zero Delay" is on or off, and then uses that to decide what the delay should be
    I mean the CSS can be changed easy here with script, yes? it's not as big a deal because it's on the same page as the source code.
    Not exactly. The CSS of any element is easy to change because Greasemonkey allows you access to the DOM of the page (the Document Object Model). It doesn't allow you access to other scripts, which aren't part of the DOM

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    28064212 wrote: »
    Ah ok, well then you just gave me the alternative idea by accident :)

    You wouldn't be able to override it without using unsafeWindow. You can't access a function or variable in a different script from a Greasemonkey one (unless you use unsafeWindow). The value would have to be held somewhere in the DOM. For example, if the checkbox "Zero Delay" had a value of "0" which was read by the cascade script, you could use the GM script to change that value to something else.

    However, the cascade script just checks whether "Zero Delay" is on or off, and then uses that to decide what the delay should be

    It's all kinda confusing :o can I think of the zerodelay value as needing to be a global variable, in order to access it with my script? it kinda sounds like it's the same idea.
    This is what's in the cascade script re zerodelay:
    if(zerodelay===!0)
    
    Is that not a check to see if it's not 0?

    This is actually getting off the original idea of what I wanted to know, but it's good to learn how it all works. :)
    28064212 wrote: »
    Not exactly. The CSS of any element is easy to change because Greasemonkey allows you access to the DOM of the page (the Document Object Model). It doesn't allow you access to other scripts, which aren't part of the DOM

    So I can change the CSS, but it's nothing to do with it being on the same page?


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    You can change the style of an element on the page, which I am 99% sure will take priority over its CSS class. It won't remove all the attributes that were set in the CSS, just the ones you've changed:

    e.g.

    CSS file has this:
    #myDiv
    {
    border: dashed red 2px;
    background-color: blue;
    }
    

    and your page has this div:

    [html]<div id="myDiv">This is my Div</div>[/html]

    Using JavaScript you can change the border from dashed red 2 pixels to a solid black 1 pixel border, and it should keep the blue background.
    document.getElementById("myDiv").style.border = 'solid black 1px';
    

    But you can't change the contents of an external CSS file (without doing something quite complex anyway).

    Edit - or you could add a new CSS file with the same class names. I don't know if it's the correct thing to do, but my experience has always been that the last CSS file takes precedence.


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    It's all kinda confusing :o can I think of the zerodelay value as needing to be a global variable, in order to access it with my script? it kinda sounds like it's the same idea.

    This is what's in the cascade script re zerodelay:
    if(zerodelay===!0)
    
    Is that not a check to see if it's not 0?
    The javascript variable zerodelay is declared as either true or false when the page is rendered (look at the source code of this page and search for zerodelay). That's handled server-side, i.e. when you request a page from boards, it checks your preferences, then sends you a page with the line "var zerodelay = false;" of "var zerodelay = true;". cascade.js can access this variable, as it's declared prior to the line <script type="text/javascript" src="http://b-static.net/javascript/cascade.js?v=1b21"></script&gt;. Greasemonkey can't access this variable as it's outside its scope.

    If you look at the HTML for the Zero Delay option in your preferences, you'll see this line: <input type="checkbox" value="on" name="zerodelay">. You can use GM to change that value, because it's available through the DOM. If that line instead read: <input type="checkbox" value="0" name="zerodelay">, that would suggest that there was an actual numerical value attached to the delay preference, so you could set the value of the input to something else to fool your settings. But in that case, the page would be rendering with a line of "var zerodelay = 100;" instead of using true or false.

    The line "if(zerodelay===!0)" isn't very clear, as it suggests zerodelay is numerical instead of boolean. It would be simpler to have "if(zerodelay)", but the writers probably had a reason for doing it that way
    This is actually getting off the original idea of what I wanted to know, but it's good to learn how it all works. :)
    Well it's just general discussion on Boards scripts, so it's all good :)
    So I can change the CSS, but it's nothing to do with it being on the same page?
    Pretty much

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    i'm sure that'll all make more sense later, when i've a less tired head on me.

    trying to think of ideas for scripts. one thing I would find handy would be to have the title of the page display the unread private message status when you get a new one. most people work in multiple tabs now, and a lot of the time I'd be off doing other things, not really on boards, so when I get a new message, I'd like the title to change to 1 unread PM, or something.

    I realise this would probably have to work with the JQuery stuff though, so I'm guessing it's not possible here. I'll have to have a think about it.


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    i'm sure that'll all make more sense later, when i've a less tired head on me.

    trying to think of ideas for scripts. one thing I would find handy would be to have the title of the page display the unread private message status when you get a new one. most people work in multiple tabs now, and a lot of the time I'd be off doing other things, not really on boards, so when I get a new message, I'd like the title to change to 1 unread PM, or something.

    I realise this would probably have to work with the JQuery stuff though, so I'm guessing it's not possible here. I'll have to have a think about it.
    Good idea for a script alright. But you don't have to worry about jQuery. A good way of approaching GM scripts is to look at the page and see if the data you need is anywhere on the page i.e. in a HTML element. In the case of the private messages count, it's on every page in the top-right. Now you just need to figure out how to access the element that contains the text, parse it, extract the number, and add it to the title

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    And check every 20 secs or whatever. I like that idea too.


  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    :D so it's a good idea? yay!

    hmmm, the checking every 20 seconds will be a new one for me.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin




  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    you guys are great, thanks!


  • Advertisement
  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    Ok, i've gotten the class under which the private message count is. and I know it's: document.getElementsByClassName("classnamehere"), but how do I actually get to see these elements? i mean obviously I throw them into an array or something, but since i want to view what's there to manipulate, before writing code and installing script....how do I? A really basic thing, I do realise, but still...

    or is it a matter of writing it into the script, and just outputting the array on the screen? to do that i'd have to go uploading the script and installing it..right?


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    Well, "alert()" calls are useful for a specific element. For example:
    alert(document.getElementsByClassName('classname')[0].innerHTML)
    
    will display the html inside the first instance of an element with that class name. Alternatively, you could create a div, position it somewhere on the page, then add text to it with a loop.

    However, in this instance, there's another way: use document.getElementsByTagName('a'). This will return an array of all the links. Use a for loop to iterate through it, searching for the Private Messages link. Once you have that object, you can use stuff like parentNode and nextSibling to get objects relative to that one, then you can go about getting the count

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    well I might explore the getElementsByClassName() anyway, but thanks. What I meant was do I have to write and upload script to view the elements, or can I just alter the URL with document.getElementsByClassName somehow to display them, but that idea seems kinda silly now.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    It depends on what condition you're looking for. Are you looking for a certain string in a span?

    For example, it sounds like you might first want to see if the element contains "Unread" as this is the span that tells you how many private messages you have. If it doesn't contain "Unread 0", then that could mean that it's telling you that there are no unread messages, and therefore you should change the title.

    This is very rough, but might give you an idea:
    var myelements = document.getElementsByClassName("classnamehere");
    for (var i = 0; i < myelements.length); i++)
    {
        // check if the element contains the word "Unread".
        if (myelements[i].innerHTML.indexOf("Unread") > 1 && myelements[i].innerHTML.indexOf("Unread 0") < 0)
        {
             // do something here.      
             break;
        }
    }
    


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Sorry, I had that written before there were more replies.

    To test scripts, I just save the page locally and write my JavaScript down the bottom of the page. Seems to work for me, but I only use chrome - not greasemonkey on firefox etc.


  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    ach, it seems so obvious when you see the code :( it's just all this elements and innerHTML stuff I've never dealt with before, it's so abstract to me for now. anyway, yes i'll save it locally I think...


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Don't worry about the syntax. That's easy to learn with a bit of practice. The important thing is knowing what it is you want to do, and then you just need to google to find out the exact wording to use.

    What I normally recommend is to write some "pseudo-code" first, so you know the outline of what it is you're doing, and then work on the syntax.
    search for all elements with a classname of "mycssclass".
    
    start loop
        if the array element's contents contains Unread then this is the correct div
            get the number of unread messages in the div 
            add it to the title of the page
            stop loop as we're done
        end if
    end loop
    

    Now you just have to think of what to google for to convert that into real script.

    Using alert() can be very handy as it gives you feedback very quickly, and also very importantly, an alert normally won't show after an error in your script.

    An error might be a mistake in the logic rather than a syntax error, so won't always show up in the console. So if you can't figure why a line of code isn't being run, put in alerts from where you know it's working, and move it down line by line until it stops appearing.


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    Eoin wrote: »
    To test scripts, I just save the page locally and write my JavaScript down the bottom of the page. Seems to work for me, but I only use chrome - not greasemonkey on firefox etc.
    It's a lot easier in Firefox. In the Greasemonkey options, you just need to set your script editor (notepad, or preferably something better). Then navigate to the page you want the script to run on, go to the Greasemonkey menu and select "New User Script". Add your code, save it, then refresh the page and it'll run.

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    I had done Eoins version last night. I have an alert, and it's popping up, but always with 0. going to try it with getElementsByTagName now...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Alert is kind of a poor man's debugger these days. Don't get me wrong, I still use it every so often. But I much prefer to use either console.log or the debugger available in Chrome (or whatever alternatives the browser you are using offers such as Firebug or built in tools in IE9)

    For instance, this video covers some of the basics in Chrome's Developer Tools:
    http://www.youtube.com/watch?v=nOEw9iiopwI


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Cool - I'm a bit out of the loop these days :)


  • Closed Accounts Posts: 3,609 ✭✭✭Boards.ie: Danny


    Don't bother trying to console.log here on Boards.ie or any other vBulletin site though, vBulletin has a lovely piece of console-log killer code in it's global.js ;)


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Don't bother trying to console.log here on Boards.ie or any other vBulletin site though, vBulletin has a lovely piece of console-log killer code in it's global.js ;)

    I can see that, replaces all the console.x methods with a blank function. I'm guessing that is to disable their debug message being emitted in production mode. That and stop errors in IE.

    Kind of annoying when libraries go monkey-patching the core though.


  • Closed Accounts Posts: 3,609 ✭✭✭Boards.ie: Danny


    I can see that, replaces all the console.x methods with a blank function. I'm guessing that is to disable their debug message being emitted in production mode. That and stop errors in IE.

    Kind of annoying when libraries go monkey-patching the core though.

    Yep, absolutely crazy stuff going on there. Would have been better off wrapping it up in a vBulletin logger and then having a debug on/off on that for production instead of this minified, half obfuscated mess of a JS library thrown in on the page. So yeah, console.log is all well and good when you know what you're doing :pac:


  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    Here's what I've got:
    alert(document.getElementsByClassName("smallfont")[0].innerHTML);
    

    I've tried numerous different versions, using ClassName, TagName, different classes and tags obviously, getting at different parts of the array etc etc. The above is not doing anything at all to my page.

    This:
    var smalltext = document.getElementsByClassName("smallfont");
    alert(smalltext.length);
    
    is alerting 0

    This:
    var bigtext = document.getElementsByTagName("div");
    alert(bigtext.length);
    
    is alerting 0 also.

    So no matter what I try it with, I have an empty array. I don't know what I'm doing wrong.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    Here's what I've got:
    alert(document.getElementsByClassName("smallfont")[0].innerHTML);
    
    I've tried numerous different versions, using ClassName, TagName, different classes and tags obviously, getting at different parts of the array etc etc. The above is not doing anything at all to my page.
    It should be. Doesn't sound like the script is running at all, just add alert("test"); at the top to be sure. Are you using Chrome or Firefox? Are you saving the page locally and adding the javascript to it directly, or using Greasemonkey?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    28064212 wrote: »
    It should be. Doesn't sound like the script is running at all, just add alert("test"); at the top to be sure. Are you using Chrome or Firefox? Are you saving the page locally and adding the javascript to it directly, or using Greasemonkey?

    It is running, I've tried with "Bla" and it works. I think it's the innerHTML part that isn't working. whenever that's in it doesn't run anything.
    I'm working in Chrome, and the page is saved locally, with the script saved on top.


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    It is running, I've tried with "Bla" and it works. I think it's the innerHTML part that isn't working. whenever that's in it doesn't run anything.
    I'm working in Chrome, and the page is saved locally, with the script saved on top.
    Ah, move it to the bottom. The element probably doesn't exist yet when the script runs

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    But then wouldn't it return something, like Undefined? I put this at the bottom, and there's now no alert box.
    var smalltext = document.getElementsByClassName("smallfont");
    alert(smalltext.length);
    


  • Registered Users, Registered Users 2 Posts: 10,912 ✭✭✭✭28064212


    But then wouldn't it return something, like Undefined? I put this at the bottom, and there's now no alert box.
    var smalltext = document.getElementsByClassName("smallfont");
    alert(smalltext.length);
    
    No, because when you call innerHTML on an undefined element, it throws an error and the script stops running. The last few lines of your HTML file should be:
    ...
    <script language="javascript">
    var smalltext = document.getElementsByClassName("smallfont");
    alert(smalltext.length);
    </script>
    </body>
    </html>
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Advertisement
  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    the problem was I had put it into an already existing script tag, with script type defined, rather than script language. So now I've it in: <script language="javascript"> and it works! :D thanks!


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    It is running, I've tried with "Bla" and it works. I think it's the innerHTML part that isn't working. whenever that's in it doesn't run anything.
    I'm working in Chrome, and the page is saved locally, with the script saved on top.

    Ha ha, I'm a big fan of bla but I like to add a h on to the end, blah. Blah, blah, blah all day long..

    Thanks to the procrastinator for the youtube link for the chrome dev tool. I use it for inspecting HTML but didn't realise I could breakpoint my javascript with it. I guess my alert("blah") is going to go the same way as my printf("hello").


  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    Hmmm. Just been thinking about this, and looking at it a bit. 28062412, you said all I have to do is find the element on the page that contains the number and go from there.... but the number will is static. it will only update when you refresh the page... so that's not going to work. I mean unless the page is getting refreshed somehow, the number of the Unread PMs is going to remain at 0.

    Now, that's a different thing - doing an auto refresh every 30seconds or so, but definitely don't want to do that. I do want to check the unread messages count, but I don't see how I can do that given the information on the page.

    Ugh, I don't know what I'm at here tbh, thinking I should just give up!


  • Closed Accounts Posts: 3,609 ✭✭✭Boards.ie: Danny


    Don't give up, what'd be the fun in that? ;)

    Have you ever noticed that if you stay on the same page on the Beta theme and somebody sends you a PM eventually your PM icon will turn orange and start flashing? Why would that happen without a refresh of the page (which we don't do :)). Look at it yourself - swap to the Beta theme, get your Browser Console's Network tab open and wait - finding this stuff out yourself will help you learn but if you don't want to do that then see spoiler below.
    We've got an endpoint you can send an ajax request to which will provide you with the unread count of PMs and Followed Threads for the currently logged in user.

    http://www.boards.ie/development/api/cascade/json/index.php

    Also, if you're going to poll this resource then poll it as little as possible, definitely no more than once per minute :)


  • Closed Accounts Posts: 14,762 ✭✭✭✭stupidusername


    Yeah I know, and I was trying stuff out myself. . . but then i come to realise I know so little, and well, I think I'm at nothing. I assumed it was something to do with JQuery, but thanks for explaining. I still don't know what's what though. might take a fresh look at it tomorrow.


Advertisement