Skip to Content

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:

         

 

         

javascript:q=''+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text);
if(!q)q=prompt('Search SDN','');if(q!=null){s=escape(q).replace(/ /g,'+');a=escape('&');m=escape('?');
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*)';} 

         

  

         

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?

         

 

         

javascript: 

                    

It isn’t ABAP 😉

         

 

         

q='' 

         

initialise a query string

         

 

         

+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text); 

         

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

         

q=''+document.selection.createRange().text; 

         

 

         

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.

         

 

         

if(!q) 

         

Did you select a text?

         

 

         

q=prompt('Search SDN',''); 

         

if not, prompt for a query string

         

 

         

if(q!=null){ 

         

if you didn’t cancel the prompt

         

 

         

s=escape(q).replace(/ /g,'+'); 

         

URL encode the string and replace spaces with +

         

 

         

a=escape('&'); 

         

URL encode the ampersand. It’s needed for the encapsulated URL in the SDN search engine

         

 

         

m=escape('?'); 

         

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.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply