1

I'm trying to figure out a way to automatically reload a browser's page until a certain sequence of characters appears in the page's HTML code. Namely, player.swf". It's a blunt and simple solution to update a page until a streaming flash player appears on a site I'm frequently visiting.

I'm a total noob in JavaScript and browser plugin programming, but I think I saw people create mini-games like shooting page elements with a small ship, that start by entering some code into the current page's address bar, and I suspect that was JavaScript (but correct me if I'm wrong).

Is there a way to create a line of code that can be placed right in the address bar and execute like described? I'm hoping this way it will work in any browser that supports executing code from address bar.

Solution that doesn't involve browser plugins and extensions (~monkeys) is very much preferred.

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • http://stackoverflow.com/questions/4163879/call-javascript-function-from-url-address-bar This SO article addresses this question to a point – Fred Oct 08 '13 at 15:14
  • Are you going to be running this inside of a c# application or a browser? – SgtPooki Oct 08 '13 at 15:49

2 Answers2

2
var i = 0, tmr = setInterval(function(re){
  $.ajax({url: document.location.href}).done(function(html){
    document.title = (++i) + ' checks';
    if ( html.match(re) ){ clearInterval(tmr); alert('found it'); document.location.reload(); }
  });
}, 60*1000, /player\.swf/i);

It doesn't reload the page exactly. It reads the page using AJAX and looks for the text of interest. If it is found, alerts the user and then does one true reload. The number of tests is tracked in the document title (visible in the page tab). Tests done every 60 seconds; adjust to your pleasure.

To make into a bookmarklet, google for "bookmarklet builder" and choose a favorite.

The code very possibly might work on the website of interest as a normal bookmarklet. If not, you probably need to include jQuery. Use this jQuery bookmarklet builder: http://benalman.com/code/test/jquery-run-code-bookmarklet/

To the guys that said it can't be done as a bookmarklet and were thinking too literally in terms of reloading the page: It can still be done with only reloading. The bookmarklet can first open a new window and then inject code into it which will reload the parent and check its innerHTML. This of course would be more complex that what I have provided.

DG.
  • 3,417
  • 2
  • 23
  • 28
  • Thanks a lot! Now I'm gonna study JavaScript to see what other cool bookmarklets can be made with it. – user1306322 Oct 08 '13 at 22:26
  • Yeah, well the thing is you sure helped me find a good place to start, but the code doesn't exactly work right away if pasted into the address bar, nor does it work correctly if processed by bookmarklet makers − it throws `done function not defined` after the first update, so it's not exactly my accepted solution yet. – user1306322 Oct 09 '13 at 10:03
  • On the jQuery bookmarklet creator that I linked, put just "1" into the "Minimum required jQuery version" box. FYI, that means "very latest of version 1". – DG. Oct 09 '13 at 11:25
  • Browser console still says `TypeError: $.ajax(...).done is not a function`. – user1306322 Oct 10 '13 at 15:08
  • Ok. I see a catch in the logic in the bookmarklet generator. It tests jquery version, and treats "1" as lower than "1.x.x" even though "1" actually means the latest version of 1. Try using "1.10.2" – DG. Oct 10 '13 at 16:23
  • I still get the same error. I don't think the problem is in the version of jQuery here. – user1306322 Oct 10 '13 at 16:41
  • It almost definitely is. I've tested the code and it works just fine in the tests I've done. I got that error only when using an outdated version of jQuery. To figure out your problem I'd need to know more details... but frankly I can't be bothered. You seem to be a programmer, so I'm sure you can figure this out. This site is here to help other programmers, not to do contract work to specification. If you can't reach your goal with the boost I've given and then accept my answer as being essentially correct, then that is just the type you are. – DG. Oct 11 '13 at 01:16
-1

You would want to create a browser plugin or tampermonkey/greasemonkey script to accomplish the 'automated' part of this.

Without a browser plugin you could certainly use just JavaScript in the address bar, or a JavaScript 'bookmarklet' as they're called.

However, you would need to click the bookmarklet, or enter the JavaScript code once for every page refresh. JavaScript is reloaded everytime a page is refreshed.. so the only way to persist that javascript code that looks for the 'player.swf' is to use a browser plugin, or something that simulates a plugin, such as tampermonkey/greasemonkey.

Both a plugin or tampermonkey/greasemonkey can run javascript automatically on page load, as long as the page's URL matches whatever is defined for that specific script. For example, here is a tampermonkey script that I wrote in about 15 seconds, and as long as it's on, it will run anytime I visit a stackoverflow.com page:

// ==UserScript==
// @name       Awesome website landing notifier
// @version    0.1
// @description  Let's you know when you're on a stack overflow webpage.
// @match      http://stackoverflow.com/*
// @copyright  2013+, Russell Dempsey aka SgtPooki
// ==/UserScript==


alert('You\'re on a stackoverflow page!');
SgtPooki
  • 11,012
  • 5
  • 37
  • 46
  • My experience with C# tells me this can be done with bookmarklets, maybe not so easily, but I'm fairly sure it's possible. And I'd like to not use any browser-dependent solutions. – user1306322 Oct 08 '13 at 15:21
  • Do you want to click the bookmarklet every time you refresh? If yes: It can be done. If no, it cannot. – SgtPooki Oct 08 '13 at 15:27
  • Also, UserScripts run in greasemonkey and/or tampermonkey. So I know for sure you can run them on either Chrome or FF. – SgtPooki Oct 08 '13 at 15:30