Posts Tagged ‘javascript’

Google search current page bookmarklet

Thursday, May 23rd, 2013

Here’s a javascript bookmarklet for googlin’ the title of the current page your viewing:


javascript:(function(){window.location.href="http://google.com/search?q="+document.title;})()

Drag me onto your bookmark bar.

Then when you’re at a sit that blocks regular visiting but allows entrance through a google search—cough, cough, nytimes.com—you can quickly google the title and click the inevitably top link.

DOM Exception in XMLHttpRequest

Thursday, April 18th, 2013

I was getting this error when trying to set up an XMLHttpRequest using javascript:


INVALID_STATE_ERR: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable

The problem was that I was calling req.setRequestHeader(...) before calling req.open(...). Reversing the order fixed the problem.

OBJ reader, mesh viewer in browser with WebGL

Wednesday, April 10th, 2013

mesh in browser

I’m getting around to release source code and demos for my PhD projects and I thought it would be cool to try out writing those demos using WebGL. After toying around (for a few years now), I have a little proof of concept.

This little mesh viewer can load an .obj mesh file (just bare basics of the format) from (1) the remote server, (2) a selected file, or (3) a drag-and-drop file. Just drag an .obj file from your folder onto the viewer! For now there’s not much interface, just a simple zoom mechanism. But I’m going to build on this and keep updated the page.

The source code is heavily borrowed from around the web. See the .js sources.

Switch search to google scholar bookmarklet

Monday, March 25th, 2013

I upgraded to Safari 6 and was happy to see the search bar gone and keyword search directly in the address bar. However I was annoyed to see that when searching you don’t see the full google url but just the query keywords. This nullified my previous hack to switch a google search to a google scholar search.

Instead here’s a little bookmarklet to switch a google search to a google scholar search:


javascript:(function(){window.location.href=window.location.href.replace("search?", "scholar?");})()

Just paste that into you address bar and create a bookmark for it. Then any time you do a normal google search, you can switch it to a google scholar search by hitting the bookmark.

source

Scroll a bit in safari window, take screenshot repeat

Friday, February 22nd, 2013

Here’s an applescript I used to make a series of screen captures of scrolling through a web page in safari:


tell application "Safari"
	activate
	set sc to 0
	repeat
		set thescript to "if((window.pageYOffset + window.innerHeight)<document.body.clientHeight)
		{
    	window.scrollBy(0,10); // horizontal and vertical scroll increments
		}"
		do JavaScript thescript in current tab of first window
		set imagePath to (path to desktop as text) & "screenCapture_" & my zero_pad(sc, 6) & ".png"
		delay 1.5
		do shell script "screencapture -o -mx -T0 " & quoted form of POSIX path of imagePath
		delay 0.5
		set sc to sc + 1
	end repeat
	
end tell

on zero_pad(value, string_length)
	set string_zeroes to ""
	set digits_to_pad to string_length - (length of (value as string))
	if digits_to_pad > 0 then
		repeat digits_to_pad times
			set string_zeroes to string_zeroes & "0" as string
		end repeat
	end if
	set padded_value to string_zeroes & value as string
	return padded_value
end zero_pad

Source

Javascript select text item from list or “Other…”

Tuesday, November 8th, 2011

Here’s a snippet of javascript that hooks up a “select” tag with a text field “input” tag. When the user changes the select automatically


function update(select,input)
{
  if(select.value == "Select...")
  {
    input.value = "";
    input.readOnly = false;
  }else if(select.value == "Other...")
  {
    input.value = "";
    input.focus();
    input.readOnly = false;
  }else
  {
    input.value = selector.value;
    input.readOnly = true;
  }
}

<select id="tf_select" name="tf_select"
onchange="update(this,document.getElementById('tf'));">
  <option value="">Select...</option>
  <option value="Male">Male</option>
  <option value="Female">Female</option>
  <option value="Other...">Other...</option>
</select>
<input name="tf" id="tf" type="text" readonly value="">

Try it here:

Getting Safari to play a short audio clip with HTML5′s audio tag

Friday, August 19th, 2011

I had a really annoying time trying to get Safari to load and play a small audio clip (mp3) I’d posted. The clip is only 2 seconds. Here’s the HTML I was using


<audio src="audio.mp3" autoplay preload="auto" controls loop>

But this resulting in nothing. Upon closer inspection I found out the the “onstalled” even was being fired so I added an “onstalled” even handler to try to load the clip again:


<audio onstalled="this.load();" src="audio.mp3" autoplay preload="auto" controls loop>

But this was to no avail, the “onstalled” event just fired each time recursively.

In the end I gave up on Safari’s ability to play/load small mp3 files. I’m not sure what the problem is since quicktime played the file fine. Also if my html and audio.mp3 files lived locally, Safari played it correctly.

I instead made use of HTML5 ability to specify fallback sources. For this I converted my mp3 file to a m4a:

First convert to wav with mplayer:


mplayer -quiet -vo null -vc dummy -ao pcm:waveheader:file="audio.wav" "audio.mp3"

Then convert to m4a with faac:


faac -o audio.m4a audio.wav

Finally use the .m4a file as a fallback source in the audio tag:


<audio autoplay preload="auto" controls loop>
<source src="audio.mp3" >
<source src="audio.m4a" >
<audio>

Ignore the onion paywall

Saturday, August 13th, 2011

Was watching the onion video channel today and to my surprise they have a paywall like nytimes. To be honest at first I thought it was a joke. But in the end it’s just as easy to get around as the nytimes one.

Just a little client side javascript gets ride of the paywall screen:


document.getElementById('gregbox-overlay').style.display = "none";
document.getElementById('gregbox-wrap').style.display = "none";

And here’s an applescript which calls that javascript on the frontmost safari window.


tell application "Safari"
	try
		set doc to front document
		do JavaScript "document.getElementById('gregbox-overlay').style.display = 'none';" in doc
		do JavaScript "document.getElementById('gregbox-wrap').style.display = 'none';" in doc
	on error errText number errNum
	end try
end tell

Horizontal and vertical centering using html, css without knowing container size

Friday, June 17th, 2011

This is an especially elusive problem when designing a simple webpage in html. There are many non solutions on the web that require knowing the size of the container element. The solution I found was straight forward and did not require knowing anything about the container. It does not require javascript (though the original code uses javascript to show off the fact that it stays centered).

A page with horizontal and vertical centering

Original source

Ignore nytimes paywall with simple client-side javascript, or applescript

Thursday, April 21st, 2011

Today was my first experience with the New York Times paywall. It manifested as an html overlay on top of the article I wanted to read. The article seemed to have fully loaded which made me think that if I could just get rid of the overlay I’d be able to read it.

This turned out to be the exactly case. I just zapped the div containing the overly (properly labeled id='overlay' and restored the overflow of the main page (to get scrolling back).

ignore nytimes paywall

This is easily accomplished with three lines of client side javascript:


document.getElementById('overlay').parentNode.innerHTML = '';
document.body.style['overflow-x'] = 'auto';
document.body.style['overflow-y'] = 'auto';

There are many ways of issuing your own javascript on pages opened in your browser, whatever that might be.

For me on a mac with safari, wrapping the above into a little applescript is easiest. I save this in a file called Ignore-nytimes-paywall.scpt:


tell application "Safari"
	try
		set doc to front document
		do JavaScript "document.getElementById('overlay').parentNode.innerHTML = '';" in doc
		do JavaScript "document.body.style['overflow-x'] = 'auto';" in doc
		do JavaScript "document.body.style['overflow-y'] = 'auto';" in doc
	on error errText number errNum
	end try
end tell

It seems that coming up with your own hack for knocking down the nytimes paywall is the trend these days. We’re all blowing our technological Joshua trumpets ;-) .

Update: I’m now more convinced that nytimes is just using this paywall as an experiment. Ignoring the paywall is even easier than I thought:

Readers need only remove “?gwh=numbers” from the URL. They can also clear their browser caches, or switch browsers as soon as they see the subscription prompt. All three of these simple fixes will let them continue reading.

Source

Update:
Seems the paywall organization has changed a little bit and now the client side javascript should be:


document.getElementById('regiwallBackground').style.display = 'none';
document.getElementById('regiwallOverlay').style.display = 'none';
document.body.parentElement.style.overflow = 'scroll';