Search code examples
javascripthtmlbookmarklet

bookmarklet: click for random specified links from a host domain


tl;dr: A bookmarklet that opens in a new tab: random link (with specified multiple html-classes) from a specified domain and code that works with current logins. Thank you.

short version of butchered code:

    javascript:
    (
        var % 20 site = domain.com
        function() {
            window.location.host == site
            void(window.open(document.links[Math.floor(document.querySelectorAll("a.class1, a.class2"))].href, '_blank'))
        }();
//beautified with: http://jsbeautifier.org/





To whom it may concern: I have searched around for a while and even considered switching services but although some come close or are similar to my particular request, none have served to address everything the request entails.

  1. Execute the script on a specific domain even when no page from said domain is currently open. If login authentication for attaining the information or data for execution is required, read or work in conjunction with existing session.

  2. Fetch from a specific domain host, a random link out of all links on that domain with a certain html-class (or indeed otherwise) using preferably, css-selectors.

  3. Open the results in a new tab.

From butchering such similarities, the result became something like this:

    //bookmarklet
javascript:
//anonymous function+ wrapped code before execution
(
// function global variables for quick substitution
  var %20 site = domain.com
function(){
//set domain for script execution
window.location.host == site
//open new tab for
void(window.open(document.links
//random link
[Math.floor
//with specific classes (elements found with css selectors)
(document.querySelectorAll("a.class1, a.class2"))
]//end random-query
.href,'_blank' //end page-open
)//end link-open
)//end "void"
}//end function defintion
//execute
();
//(tried) checked with: 
//http://www.javascriptlint.com/online_lint.php

Lastly, i have attained at most, basic css knowledge. I apologise if this request has anybody headdesking, palming or otherwise in gtfo mode. It is only too sad there is apparently no tag for "Warning: I DIY-ed this stuff" in StackExchange. However, i still would like answers that go into a bit of depth of explaining why and what each correction and modification is.

Thank you presently, for your time and effort.


Solution

  • Theoretically, the following code should do what you want:

    window.addEventListener('load', function ( ) {
        var query = 'a.class1[href], a.class2[href]';
        var candidates = document.querySelectorAll(query);
        var choice = Math.floor(Math.random() * candidates.length);
        window.open(candidates.item(choice).href, 'randomtab');
    }, true);
    window.location.href = 'http://domain.com';
    

    But it doesn't, because the possibility to retain event listeners across a page unload could be abused and browsers protect you against such abuse.

    Instead, you can manually load the domain of your choice and then click a simpler bookmarklet with the following code:

    var query = 'a.class1[href], a.class2[href]';
    var candidates = document.querySelectorAll(query);
    var choice = Math.floor(Math.random() * candidates.length);
    window.open(candidates.item(choice).href, 'randomtab');
    

    You could wrap the above in javascript:(function ( ) { ... })(); and minify as before, but it already works if you just minify it and only slap a javascript: in front.

    I understand your situation of being an absolute beginner and posting "DIY" code, but I'm still not going to explain step-by-step why this code works and yours doesn't. The first version of the code above is complex to explain to a beginner, and the list of issues with the code in the question is too long to discuss all of them. You'll be better off by studying more Javascript; a good resource with tutorials is MDN.