Works perfectly, though method is a bit strange.

About: Google old favicon
September 2015 modifié dans Discussions de script Chrome

It looks a bit strange to have a while loop just to iterate i.
Personally I would write it like this:

icon = 'data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAICAgAAAAP8AAP8AAAD//wD/AAAA/wD/AP//AAD///8A//3/AP39/wD6/f8A+P3/AP/8/wD9/P8A+vz/AP/7/wD/+v8A/vr/APz6/wD4+v8A+/n/APP5/wD/+P8A+vj/AO/4/wDm+P8A2fj/AP/3/wD/9v8A9vb/AP/1/wD69f8A9PT/AO30/wD/8/8A//L/APnx/wD28P8A///+APj//gD2//4A9P/+AOP//gD//f4A6f/9AP///AD2//wA8//8APf9/AD///sA/v/7AOD/+wD/+vsA9/X7APr/+gDv/voA///5AP/9+QD/+/kA+e35AP//+ADm//gA4f/4AP/9+AD0+/gA///3APv/9wDz//cA8f/3AO3/9wD/8fcA//32AP369gDr+vYA8f/1AOv/9QD/+/UA///0APP/9ADq//QA///zAP/18wD///IA/fzyAP//8QD///AA9//wAPjw8AD//+8A8//vAP//7gD9/+4A9v/uAP/u7gD//+0A9v/tAP7/6wD/+eoA///pAP//6AD2/+gA//nnAP/45wD38eYA/fblAP/25AD29uQA7N/hAPzm4AD/690AEhjdAAAa3AAaJdsA//LXAC8g1gANH9YA+dnTAP/n0gDh5dIADyjSABkk0gAdH9EABxDRAP/l0AAAJs4AGRTOAPPczQAAKs0AIi7MAA4UywD56soA8tPKANTSygD/18kA6NLHAAAjxwDj28QA/s7CAP/1wQDw3r8A/9e8APrSrwDCtqoAzamjANmPiQDQj4YA35mBAOmefgDHj3wA1qR6AO+sbwDpmm8A2IVlAKmEYgCvaFoAvHNXAEq2VgA5s1UAPbhQAFWtTwBStU0ARbNNAEGxTQA7tEwAObZIAEq5RwDKdEYAULhDANtuQgBEtTwA1ls3ALhgMQCxNzEA2FsvAEC3LQB0MCkAiyYoANZTJwDLWyYAtjMlALE6JACZNSMAuW4iANlgIgDoWCEAylwgAMUuIAD3Vh8A52gdALRCHQCxWhwAsEkcALU4HACMOBwA0V4bAMYyGgCPJRoA218ZAJM7FwC/PxYA0msVAM9jFQD2XBUAqioVAIAfFQDhYRQAujMTAMUxEwCgLBMAnxIPAMsqDgCkFgsA6GMHALE2BAC9JQAAliIAAFYTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8AsbGxsbGxsbGxsbGxsbGxd7IrMg8PDw8PDw8PUBQeJXjQYE9PcKPM2NfP2sWhcg+BzTE7dLjbmG03YWaV4JYye8MPbsLZlEouKRRCg9SXMoW/U53enGRAFzCRtNO7mTiAyliw30gRTg9VbJCKfYs0j9VmuscfLTFbIy8SOhA0Inq5Y77GNBMYIxQUJzM2Vxx2wEmfyCYWMRldXCg5MU0aicRUms58SUVeRkwjPBRSNIfBMkSgvWkyPxVHFIaMSx1/0S9nkq7WdWo1a43Jt2UqgtJERGJ5m6K8y92znpNWIYS1UQ89Mmg5cXNaX0EkGyyI3KSsp6mvpaqosaatq7axsQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';

for (var i = 0; i++ < document.head.children.length;)
    if (document.head.children[i].getAttribute('rel') == "shortcut icon") {
        document.head.children[i].href = icon;
        break;
    }

Because you can always just break out of the for loop after you find the correct element.

And I think it's not necessary to check whether oChildren[i].tagName == 'LINK'

I also made a one-liner that works:

document.head.querySelector('link[rel="shortcut icon"]').href = icon;

The only issue being that the querySelector function seems to be a bit slow.

I also checked what element was actually being replaced (the value of i), and it seemed to be value 1 - Assuming the value is always going to be 1, this should work:

document.head.children[1].href = icon;

Réponses

  • I consider the word "break" as deprecated, personally it reminds me the days of GOTO. This is a classic WHILE since WHILE denotes a special condition other than enumeration. While a FOR loop normally implies N times something.

    About the children[1], this was my first reaction, but I quickly voted against that since I desire scripts that works for life not months or years.
  • September 2015 modifié Opera

    Regarding break statements, it is considered good practice to not rely on them to exit from loops, in case the condition which it should break is not met.
    But if you already have a main exit condition (eg end of the array), then break could be useful to exit from the loop early, if it is not necessary to continue iterating through the rest of the array (eg if you only want to find one particular element, and you have actually found it).

    The for loop would still work fine without the break statement, but it would just keep iterating unnecessarily to the end of the array (it would also iterate to the end of the array if it doesn't find the element, and then exit normally without making any changes)

    I definitely wouldn't place break or continue in the same league as goto; break and continue are at least sometimes appropriate. But goto should always be considered depracated.

    Compared to a while loop, I think a for loop is basically the same thing (loop until condition is met), except it also allows declaring temporary variables.

    So basically it just means you have to use the temporary variable inside the loop itself, which means for (var i = 0; document.head.children[i].getAttribute('rel') != "shortcut icon" && i++ < document.head.children.length;) wouldn't work, since the variable i would no-longer exist when you want to use it.

    But it also means you don't have to check if document.head.children[i].getAttribute('rel') != "shortcut icon" twice.

    I also did consider writing the for loop like this: for (var i = document.head.children.length; i--;)

    But I decided that since the matching element is closer to zero than the end of the array, it's better to start from zero, so it will iterate less times.

     

    And I think I agree that it's probably safer to not hardcode it to use children[1], but to actually check which element it should use.

  • Well surely Google will always have a shortcut icon. But I hates scripts that can through uncaught exception. This if  is there just for "good manners" motive.

    Maybe w/o the if  my script looks more elegent (no notFound  variable, no break), but I rather keep it rather than adding a variable or using break.

    Agree about the direction. My toughts exactly.
  • September 2015 modifié Opera

    In the for loop I made, the break statement is basically just there for CPU efficiency.
    Without it, the favicon still gets replaced, but it keeps iterating until the end of the array anyway - it doesn't become an infinite loop.

    And if the favicon is not found, it just iterates to the end of the array regardless and doesn't change anything.

    So basically, this works:

    for (var i = 0; i++ < document.head.children.length;)
        if (document.head.children[i].getAttribute('rel') == "shortcut icon") {
            document.head.children[i].href = icon;
        }
    

    It just means that it iterates through the loop more times than it needs to.

Connectez-vous ou Inscrivez-vous pour répondre.