Despite the numerous positive reactions to the Firefox search plugin for SDN, I wasn’t 100% satisfied. As mentioned in an earlier article, cross platform/cross browser applications are always a target to aim for. The Firefox plugin doesn’t comply to that at all, and MIE and Opera users were left out in the cold. Don’t despair though; the rescue comes in the next paragraph.
As you know by now, I have a weak spot for concise code. The print bookmarklets for printing forums and weblogs are a good example of this. So I wondered whether with a bookmarklet I could establish the same as with the plugin. Would the user also be able to select any text in his browser as input for the SDN search engine? The answer to both is yes. And with no further ado, here it is:
Just copy and paste (as one line!) and save the code as a bookmarklet. Copying text from a MIE – we will see further on that this causes other problems too – window can sometimes be a bit tedious, so in order to make things easier, here is a file with the same code.
But what does this lengthy, unindented and not so easy to read code actually do, you might wonder?
It isn’t ABAP 😉
initialise a query string
and concatenate the selected text. This is a cross browser nested test and text selection. Selecting text is different for each type of browser:
– Mozilla, Firefox and Safari like window.getSelection
– MIE for Mac, Mozilla, Firefox, Netscape and iCab like document.getSelection
– MIE for Win likes document.selection.createRange().text
As mentioned earlier, MIE is a troublemaker. The bookmarklet won’t work in MIE as such. Not that the code isn’t legit, but it won’t interpret the whole bunch due to the length. Size does matter and even when using short variable names and leaving out every possible space, it’s too long.
In order to solve the problem, you need to change this line in only
You might do this anyway for any browser if you don’t care about cross browser/cross platform capabilities. Be sure that you select the correct statement for your browser though.
Did you select a text?
if not, prompt for a query string
if you didn’t cancel the prompt
URL encode the string and replace spaces with +
URL encode the ampersand. It’s needed for the encapsulated URL in the SDN search engine
URL encode the question mark. It’s needed for the encapsulated URL in the SDN search engine
location='https://www.sdn.sap.com/sdn/search.sdn?contenttype=url&query='+s +'&selected=0&content=/irj/servlet/prt/portal/prtroot/pcd!3aportal_content!2fSDN!2fiViews! 2fWCM!2fcom.sap.sdn..wcm.search.search_adv'+m+'prttheme%3DCSIN'+a+'QueryString='+s +a+'SearchPluginName=sdn_all'+a+'SelectedCustomProps=resourcetype(value=s*)';}
Finally use the SDN search URL and use the query and URL encoded ampersand/question mark to make things complete.
That’s it. Again, no rocket science is required in order to make life a little bit easier and more fun.