Sign up | Lost password? | Help

[ advanced search ]

Googlepedia - Wikipedia on page results Google search

Forums » Opera Community » General Opera topics » User JavaScript


Go to last post

Sunday, 4. June 2006, 08:38:51

adas

Moderator PL

avatar

Posts: 3841

Poland

Googlepedia - Wikipedia on page results Google search

Firefox has a Googlepedia: https://addons.mozilla.org/firefox/2517/

This add to google page with articles from Wikipedia. I saw that is only GreaseMonkey script. Would somebody try to transfer it to UserJS? I think that it won't be difficult for Opera users knowing a lot about JS.

I unpacking files from Firefox extension and send to → http://adamdziura.9g.pl/googlepedia/content/

Screenshots → https://addons.mozilla.org/firefox/2517/previews/

Sunday, 4. June 2006, 08:43:35

Whoa. Brilliant!
+1!

Monday, 5. June 2006, 13:37:34

adas

Moderator PL

avatar

Posts: 3841

Poland

Originally posted by YtseJam:

+1!


But who write script for Opera?

Monday, 5. June 2006, 16:04:07

scipio

Undutchable

avatar

Posts: 29747

Netherlands

I don't think it's possible to create a UserScript that does exactly the same thing due to cross-domain restrictions. But here's a first try to get something similar. It still needs some improvements.

if(window.location.href.indexOf('.google.')>-1){
    var st=window.location.href.match(/q=(.*?)&/);   
    if(st){
        st=st[1]+'';
        var strWiki='http://en.wikipedia.org/wiki/';
        
        window.opera.addEventListener(
    'AfterEventListener.load',
    function(){var t=document.getElementsByTagName('table');if(!t)return;var i=0;while(t[i].getAttribute('bgcolor')!='#e5ecf9')i++;t=t[i];if(t.getElementsByTagName('td')[1].innerHTML.indexOf('WikiPedia')<0){t.getElementsByTagName('td')[0].appendChild(document.createElement('br'));t.getElementsByTagName('td')[0].appendChild(t.getElementsByTagName('td')[1].removeChild(t.getElementsByTagName('td')[1].childNodes[0]));t.getElementsByTagName('td')[1].appendChild(document.createTextNode('WikiPedia'));t.getElementsByTagName('td')[1].style.fontWeight='bold';t.getElementsByTagName('td')[1].style.fontSize='larger';t=t.nextSibling;while(t.nodeType!=1)t=t.nextSibling;
        var newT=document.createElement('table');newT.appendChild(document.createElement('tr'));for(i=0;i<2;i++)newT.childNodes[0].appendChild(document.createElement('td'));t.parentNode.insertBefore(newT,t);newT=newT.childNodes[0].childNodes[0];newT.style='width:48%';var tempT;while(t.nodeType!=1 || t.getAttribute('id')!='navbar'){tempT=t.nextSibling;newT.appendChild(t.parentNode.removeChild(t));t=tempT};newT=newT.nextSibling;var ifr=document.createElement('iframe');ifr.src=strWiki+st;newT.style='border-left:solid 1px #3366cc;vertical-align:top;height:100%;';ifr.style='height:100%;width:100%;border:none;';newT.appendChild(ifr);newT.parentNode.style='height:100%';newT.parentNode.parentNode.style='height:500px';}
    },false);
    }
}

if(window.location.href.indexOf('.wikipedia.org')>-1){
    window.opera.addEventListener(
        'BeforeScript',
        function(ev){if(ev.element.src=='http://en.wikipedia.org/skins-1.5/common/wikibits.js?1')ev.element.text=ev.element.text.replace('window.top != window','1==0');},
        false);
}

Monday, 5. June 2006, 19:12:07 (edited)

kokita

avatar

Posts: 68

Very nice Scipio. It goes even fine with Grypen's filters for Google. Good work :up:

Just a bit of feedback:

1. The query on Wikipedia seems to be performed with the string of the current Google url, but it includes the '+' symbol between separate words in the Google query. This fools wikipedia, which would never be able to find 'Opera+browser' when the search at Google was 'Opera browser'. See it in the attachment. I'm sure yo can fix it in no time Homer: Doh!

googlepedia.PNG

2. When switching beetween pages and returning to the 'googlepedied' page, sometimes the Google part of it is way down the place where it should be. I mean, it is not inmediately under the blue band which reads 'Results xxx of about xxx for xxxxxxx (xxx seconds)' but something like one whole page down. This is a bit annoying.

3. And most important. This script makes me remind of my (not so) old dream for Opera, which is something like the Antipagination and Repagination extensions for Firefox. I remember that we discussed it some time ago here (with some input by Scipio also). They basically append a given url (which is specified by the user through a context menu entry on the page) to the end of the current page. It also does this recurrently for pages such as forum pages which are split into several pages all of which are linked through a 'Next' link. The pages keep they normal size and look. I'm also sure that any of the JS masters around here can do it in no time :whistle:

By the way, the problem with that is that Opera (neither Firefox or IE, by the way), seem to be able to save the content of the iframes, which seem to be the way those scripts (includind yours, if i'm not wrong) load the 'foreing' page. Fortunately enough, Scrapbook does this and maybe Dmitry Antonyuk's OBook for Opera can achieve it for us also in the future.

Cheers!

Tuesday, 6. June 2006, 19:28:22

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by kokita:

1. The query on Wikipedia seems to be performed with the string of the current Google url, but it includes the '+' symbol between separate words in the Google query.


Fixed in my new "internal" build. :wink: Will post it after looking at some of the other issues.

Originally posted by kokita:

2. When switching beetween pages and returning to the 'googlepedied' page


Can you post a screenshot of that?

Originally posted by kokita:

3. And most important. This script makes me remind of my (not so) old dream for Opera, which is something like the Antipagination and Repagination extensions for Firefox.


I'll have to look for the Opera Forums' threads you mention, because I'm not really familiar with these FF extensions.

Btw, there's another problem that I found: the google results look bad when there are ads on the right side of the page.

Tuesday, 6. June 2006, 21:58:55 (edited)

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Hmm, still not fully satisfied. It looks like the following script only shows all Google results after resizing the page. Can anyone confirm that? The Wikipedia articles are shown in a much more convenient way though (i.e. all space is reserved for the article).

if(window.location.href.indexOf('.google.')>-1){
    var st=window.location.href.match(/q=(.*?)&/);   
    if(st){
        st=st[1].replace('+','_')+'';
        var strWiki='http://en.wikipedia.org/wiki/';
        
        window.opera.addEventListener(
    'AfterEventListener.load',
    function(){var t=document.getElementsByTagName('table');if(!t)return;var i=0;while(t[i].getAttribute('bgcolor')!='#e5ecf9')i++;t=t[i];if(t.getElementsByTagName('td')[1].innerHTML.indexOf('WikiPedia')<0){t.getElementsByTagName('td')[0].appendChild(document.createElement('br'));t.getElementsByTagName('td')[0].appendChild(t.getElementsByTagName('td')[1].removeChild(t.getElementsByTagName('td')[1].childNodes[0]));t.getElementsByTagName('td')[1].appendChild(document.createTextNode('WikiPedia'));t.getElementsByTagName('td')[1].style.fontWeight='bold';t.getElementsByTagName('td')[1].style.fontSize='larger';t=t.nextSibling;while(t.nodeType!=1)t=t.nextSibling;
        var newT=document.createElement('table');newT.appendChild(document.createElement('tr'));for(i=0;i<2;i++)newT.childNodes[0].appendChild(document.createElement('td'));t.parentNode.insertBefore(newT,t);newT=newT.childNodes[0].childNodes[0];newT.style='width:48%;height:100%';var tempT;if(t.nodeType==1 && t.nodeName=='DIV')t=t.childNodes[0];while(t.nodeType!=1 || t.getAttribute('id')!='navbar'){tempT=t.nextSibling;newT.appendChild(t.parentNode.removeChild(t));t=tempT};newT=newT.nextSibling;var ifr=document.createElement('iframe');ifr.src=strWiki+st;newT.style='/*border-left:solid 1px #3366cc;vertical-align:top;height:100%;padding:0 0 0 10px;';ifr.style='height:100%;width:100%;border:none;';newT.appendChild(ifr);newT.parentNode.style='height:100%';}
    },false);
    }
}

if(window.location.href.indexOf('.wikipedia.org')>-1){
    window.opera.addEventListener(
        'BeforeScript',
        function(ev){if(ev.element.src=='http://en.wikipedia.org/skins-1.5/common/wikibits.js?1')ev.element.text=ev.element.text.replace('window.top.location = window.location;','window.addEventListener("load",function(){var d=document.getElementById("content");d.style.margin="0";d=document.getElementById("footer");d.style.display="none";d=document.getElementsByTagName("div");for(var i=0,di;di=d[i];i++)if(di.className=="portlet")di.style.display="none";},false);');},
        false);
}

Tuesday, 6. June 2006, 20:53:49

adas

Moderator PL

avatar

Posts: 3841

Poland

Originally posted by scipio:

Btw, there's another problem that I found: the google results look bad when there are ads on the right side of the page.


Maybe hide this ads: table[width="25%"] {display:none} ?

And this script needs some css for wiki (hide menus) from FF script.

Tuesday, 6. June 2006, 20:58:23

I don't know if it's possible to do this in this UserJS, but, the script doesn't take into account searchs made in another language.

Lets say I search in google.com for "דרים תי'אטר" which is the Hebrew way of writing "Dream Theater".
The googlepedia UserJS relsults will obviously show nothing for this, because it doesn't exist in en.wikipedia.org, but rather in he.wikipedia.org.

Is there some way to trick the search?

Tuesday, 6. June 2006, 21:04:18

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by YtseJam:

Is there some way to trick the search?


I saw how the FF extension tries to deal with that and it's something that can work in UserJS too (I even think the code for that is reusable in the above UserScript). I figured it could be saved for later. Basic functionality first. :wink:

Originally posted by adas:

Maybe hide this ads: table[width="25%"] {display:none} ?

Yes, it's probably as simple as that. I'll take a look.

And this script needs some css for wiki (hide menus) from FF script.


I'm not sure I understand. Can you explain?

Tuesday, 6. June 2006, 21:09:28

kokita

avatar

Posts: 68

Originally posted by scipio:

Originally posted by kokita:

2. When switching beetween pages and returning to the 'googlepedied' page


Can you post a screenshot of that?



Here it is. It is taken with the new version you posted today. To quickly reproduce it, launch a search and zoom in or out the page. You'll see then how the Google part of the page goes down:

googlepediamess.PNG

I also noticed that there is an ugly scroll bar that appears suddenly on the Wikipedia side of the page sometimes. Maybe that is of your interest :smile:

Originally posted by scipio:

Originally posted by kokita:

3. And most important. This script makes me remind of my (not so) old dream for Opera, which is something like the Antipagination and Repagination extensions for Firefox.


I'll have to look for the Opera Forums' threads you mention, because I'm not really familiar with these FF extensions.



There is a link to the thread on my previous post. It is really really useful stuff.

So, thank you Scipio for fixing the url bug. Cheers!

Tuesday, 6. June 2006, 21:19:44 (edited)

And this script needs some css for wiki (hide menus) from FF script.

I'm not sure I understand. Can you explain?



Take a look at the screen shots here: https://addons.mozilla.org/firefox/2517/previews/
Unlike the FF extensions, in the UserJS the wikipedia menu to the left is shown.

Tuesday, 6. June 2006, 21:14:35

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by YtseJam:

Unlike the FF extensions, in the UserJS the wikipedia menu to the left is show.


Not in today's version. :smile:

Tuesday, 6. June 2006, 21:37:56

adas

Moderator PL

avatar

Posts: 3841

Poland

Originally posted by scipio:

I'm not sure I understand. Can you explain?


Add to Wikipedia this css:
#column-one {display:none !important}
#content, #column-content {margin:0 !important}

Tuesday, 6. June 2006, 21:58:15

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by adas:

Add to Wikipedia this css:
#column-one {display:none !important}
#content, #column-content {margin:0 !important}


I did it differently but the result is the same: see the last script I posted above.

screenshot

Tuesday, 6. June 2006, 22:07:40

adas

Moderator PL

avatar

Posts: 3841

Poland

Originally posted by scipio:

I did it differently but the result is the same: see the last script I posted above.


But the last script redirected me to article in Wikipedia... :wink: i don't know why... But now works... :confused:

Wednesday, 7. June 2006, 19:31:23

scipio

Undutchable

avatar

Posts: 29747

Netherlands

So what about the number of Google results you see on the page? Do you miss any?

Wednesday, 7. June 2006, 21:00:56

This is how it currently looks (+a problem marked in the screen shot + it looks like the google links to the next pages seem not to sit at the bottom of the page?)

Was any work done on the other pending features, scipio? :smile:

Wednesday, 7. June 2006, 21:19:03

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by YtseJam:

Was any work done on the other pending features, scipio?


Haven't had any time today, but I haven't forgotten about them. :wink:

Thanks for the screenshot. Which Opera version is that?

Monday, 12. June 2006, 19:21:11

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by YtseJam:

Bump!


I think you're waiting for the "localized" version, right? This new version takes into account which Google language you use (the script bits were taken from the original GooglePedia script for FF). The WikiPedia results now only take half of the screen height, which possibly prevents the scrollbar errors from ocurring.

if(window.location.href.indexOf('.google.')>-1){
    var st=window.location.href.match(/q=(.*?)&/);   
    if(st){
        st=st[1].replace('+','_')+'';
           
        // taken from the original GooglePedia script
        var googleUrl = window.location.href.match(/http:\/\/(.*?)\//);
        googleUrl = 'http://'+googleUrl[1]+'/';

        var googleTld = window.location.href.match(/http:\/\/(.*?)google\.(.*?)\//);
        var googleTlds = { 'com':'en', 'co.uk':'en', 'co.jp':'ja' };
        var wikiSubdomain = googleTlds[googleTld[2]];
        if(!wikiSubdomain) {wikiSubdomain = googleTld[2];}
        // -->
        
        //var strWiki='http://en.wikipedia.org/wiki/';
        var strWiki='http://'+wikiSubdomain+'.wikipedia.org/wiki/'
        
        window.opera.addEventListener(
    'AfterEventListener.load',
    function(){
        // change results header
        var t=document.getElementsByTagName('table');
        if(!t)return;var i=0;
        while(t[i].getAttribute('bgcolor')!='#e5ecf9')i++;
        t=t[i];
        if(t.getElementsByTagName('td')[1].innerHTML.indexOf('WikiPedia')<0){
            t.getElementsByTagName('td')[0].appendChild(document.createElement('br'));
            t.getElementsByTagName('td')[0].appendChild(t.getElementsByTagName('td')[1].removeChild(t.getElementsByTagName('td')[1].childNodes[0]));
            t.getElementsByTagName('td')[1].appendChild(document.createTextNode('WikiPedia'));
            t.getElementsByTagName('td')[1].style.fontWeight='bold';
            t.getElementsByTagName('td')[1].style.fontSize='larger';
            t=t.nextSibling;
            while(t.nodeType!=1)t=t.nextSibling;
        
            var ifr=document.createElement('iframe');
            ifr.src=strWiki+st;
            ifr.style='width:50%;height:50%;float:right;border:solid 1px #aaa;margin:0 0 20px 20px';
            t.parentNode.insertBefore(ifr,t);
            }
    },false);
    }
}

if(window.location.href.indexOf('.wikipedia.org')>-1){
    window.opera.addEventListener(
        'BeforeScript',
        function(ev){
            // remove function that makes it impossible to open wikipedia in a frame and add function to hide unnecessary page elements
            if(ev.element.src.indexOf('.wikipedia.org/skins-1.5/common/wikibits.js?1')>-1)ev.element.text=ev.element.text.replace('window.top.location = window.location;','window.addEventListener("load",function(){var d=document.getElementById("content");d.style.margin="0";d.style.border="none";d.style.fontSize="11px";d=document.getElementById("footer");d.style.display="none";d=document.getElementsByTagName("div");for(var i=0,di;di=d[i];i++)if(di.className=="portlet")di.style.display="none";},false);');},
        false);
}

Monday, 12. June 2006, 19:39:46

Current look. I think the height can be adjusted a bit more?

As for the localization bit, see screen shot (you probably didn't take into account RTL :wink:)
1. an error is showed because it looks for co.il.wikipedia.org/wiki which of course doesn't exist. It's suppposed to be he.wikipedia.org/wiki...

2. The Googlepedia bit should be in the left section of the screen and not right section.

3. What I thought it'd be like, is: if I'm searching from google.com (English version of Google) for something in Hebrew, then it would look in he.wikipedia.org, but from what I can tell from your doings, it can only be achived if I'm looking for something in Hebrew from google.co.il? Because otherwise it looks for the Hebrew term in en.wikipedia.org, which there it will obviously not find it.

Monday, 12. June 2006, 19:52:10

adas

Moderator PL

avatar

Posts: 3841

Poland

I waiting for a link to hide/unhide this iframe.
When article doesn't exist, script should search a other article (maybe if in google results exist link to wikipedia script open this link).

Sorry for my english... Ehh...

Monday, 12. June 2006, 19:59:07

A hide/Show button (much like in the My.Opera Community Enhancement script would be sweet indeed.

Giving it a default action to set in the script (on or off by default) would be even sweeter. :smile:

Monday, 12. June 2006, 20:21:40

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by YtseJam:

1. an error is showed because it looks for co.il.wikipedia.org/wiki which of course doesn't exist. It's suppposed to be he.wikipedia.org/wiki...


A similar problem occurs when using google.co.uk, which of course should go to en.wikipedia.org rather than co.uk.wikipedia.org. Fixed now.

Originally posted by YtseJam:

2. The Googlepedia bit should be in the left section of the screen and not right section.


It may be a bit complicated to come up with a generic solution, so I made the script put the WikiPedia box on the left at google.co.il.

Originally posted by YtseJam:

3. What I thought it'd be like, is: if I'm searching from google.com (English version of Google) for something in Hebrew, then it would look in he.wikipedia.org, but from what I can tell from your doings, it can only be achived if I'm looking for something in Hebrew from google.co.il? Because otherwise it looks for the Hebrew term in en.wikipedia.org, which there it will obviously not find it.


I don't know if it's possible to have it the way you describe. For now it is based on the the google extension.

Originally posted by adas:

I waiting for a link to hide/unhide this iframe.


Click on the word WikiPedia to toggle the WikiPedia results. Of course, you need the script below:

Originally posted by YtseJam:

Giving it a default action to set in the script (on or off by default) would be even sweeter.


edited to incorporate that wish. set the variable ShowWikipediaByDefault to true or false.

if(window.location.href.indexOf('.google.')>-1){
    var st=window.location.href.match(/q=(.*?)&/);   
    if(st){
        st=st[1].replace('+','_')+'';
        
        var ShowWikipediaByDefault = false;
           
        // taken from the original GooglePedia script
        var googleUrl = window.location.href.match(/http:\/\/(.*?)\//);
        googleUrl = 'http://'+googleUrl[1]+'/';

        var googleTld = window.location.href.match(/http:\/\/(.*?)google\.(.*?)\//);
        var googleTlds = { 'com':'en', 'co.uk':'en', 'co.jp':'ja', 'co.il':'he' };
        var wikiSubdomain = googleTlds[googleTld[2]];
        if(!wikiSubdomain) {wikiSubdomain = googleTld[2];}
        // -->
        
        //var strWiki='http://en.wikipedia.org/wiki/';
        var strWiki='http://'+wikiSubdomain+'.wikipedia.org/wiki/'
        
        window.opera.addEventListener(
    'AfterEventListener.load',
    function(){
        // change results header
        var t=document.getElementsByTagName('table');
        if(!t)return;var i=0;
        while(t[i].getAttribute('bgcolor')!='#e5ecf9')i++;
        t=t[i];
        if(t.getElementsByTagName('td')[1].innerHTML.indexOf('WikiPedia')<0){
            t.getElementsByTagName('td')[0].appendChild(document.createElement('br'));
            t.getElementsByTagName('td')[0].appendChild(t.getElementsByTagName('td')[1].removeChild(t.getElementsByTagName('td')[1].childNodes[0]));
            var tlink = document.createElement('a');
            tlink.setAttribute('onclick','javascript:document.getElementById("googlepedia_iframe").style.display=(document.getElementById("googlepedia_iframe").style.display=="block")?"none":"block"');
            tlink.appendChild(document.createTextNode('WikiPedia'));
            t.getElementsByTagName('td')[1].appendChild(tlink);
            t.getElementsByTagName('td')[1].style.fontWeight='bold';
            t.getElementsByTagName('td')[1].style.fontSize='larger';
            t=t.nextSibling;
            while(t.nodeType!=1)t=t.nextSibling;
        
            // add WikiPedia results
            var ifr=document.createElement('iframe');
            ifr.src=strWiki+st;
            ifr.setAttribute('id','googlepedia_iframe');
            if(wikiSubdomain=='he'){ifr.style='width:50%;height:50%;float:left;border:solid 1px #aaa;margin:0 20px 20px 0';}
            else {ifr.style='width:50%;height:50%;float:right;border:solid 1px #aaa;margin:0 0 20px 20px';};
            ifr.style.display=(ShowWikipediaByDefault)?"block":"none";
            t.parentNode.insertBefore(ifr,t);
            }
    },false);
    }
}

if(window.location.href.indexOf('.wikipedia.org')>-1){
    window.opera.addEventListener(
        'BeforeScript',
        function(ev){
            // remove function that makes it impossible to open wikipedia in a frame and add function to hide unnecessary page elements
            if(ev.element.src.indexOf('.wikipedia.org/skins-1.5/common/wikibits.js?1')>-1)ev.element.text=ev.element.text.replace('window.top.location = window.location;','window.addEventListener("load",function(){var d=document.getElementById("content");d.style.margin="0";d.style.border="none";d.style.fontSize="11px";d=document.getElementById("footer");d.style.display="none";d=document.getElementsByTagName("div");for(var i=0,di;di=d[i];i++)if(di.className=="portlet")di.style.display="none";},false);');},
        false);
}

Monday, 12. June 2006, 20:59:02

Originally posted by scipio:

Originally posted by YtseJam:

2. The Googlepedia bit should be in the left section of the screen and not right section.


It may be a bit complicated to come up with a generic solution, so I made the script put the WikiPedia box on the left at google.co.il.


Okay, hopefully this could be addressed at some point.

Originally posted by scipio:

Originally posted by YtseJam:

3. What I thought it'd be like, is: if I'm searching from google.com (English version of Google) for something in Hebrew, then it would look in he.wikipedia.org, but from what I can tell from your doings, it can only be achived if I'm looking for something in Hebrew from google.co.il? Because otherwise it looks for the Hebrew term in en.wikipedia.org, which there it will obviously not find it.


I don't know if it's possible to have it the way you describe. For now it is based on the the google extension.


Okay, hopefully this could be addressed at some point as well. :smile:

These are probably the two "major" issues preventing me to present this script for the Hebrew community.
It is realy quite a valueable tool.

Good job!

Thursday, 15. June 2006, 10:10:18

adas

Moderator PL

avatar

Posts: 3841

Poland

I change link to wikipedia from:
var strWiki='http://'+wikiSubdomain+'.wikipedia.org/wiki/'

to:
var strWiki='http://'+wikiSubdomain+'.wikipedia.org/wiki/Special:Search?go=Go&search='


and now, when article doesn't exist script redirect to page with Search in Wikipedia.
googlepedia.js

And know I want a link: "open article in this tab" (because sometimes i want a editing)

Thursday, 15. June 2006, 19:22:20

adas

Moderator PL

avatar

Posts: 3841

Poland

Ehh... my script send a keyword to wikipedia search with _

Like: Opera_browser - not Opera browser.

And know doesn't work good. I can't change space to %20...

Saturday, 17. June 2006, 20:44:00

Another thing that I find missing is a link of sorts to open the wiki in a new tab.

Sunday, 18. June 2006, 13:20:48 (edited)

scipio

Undutchable

avatar

Posts: 29747

Netherlands

New version, with a few small fixes and taking into account the following remarks:

Originally posted by YtseJam:

Another thing that I find missing is a link of sorts to open the wiki in a new tab.


Originally posted by adas:

and now, when article doesn't exist script redirect to page with Search in Wikipedia.



Don't forget to check the User Settings in the first lines of the script; there are currently 5 things that can be changed:
* show Wikipedia by default
* set height of WikiPedia results
* use search link proposed by adas or the link used originally (adas' link is recommended; it's what I use in my search.ini as well).
* make the "open" link open WikiPedia in a new tab or in the same tab.
* toggle removal of Google ads

if(window.location.href.indexOf('.google.')>-1){
    var st=window.location.href.match(/q=(.*?)&/);   
    if(st){
        // USER SETTINGS
        // the Wikipedia iframe can be shown and hidden by clicking on 'Wikipedia'; this setting sets the initial state
        var ShowWikipediaByDefault = false; 
        
        // height of Wikipedia iframe as percentage of window height
        var HeightOfWikipediaPage = '80%';
        
        // uses Google search terms for a Wikipedia search if true; will try to match an existing Wikipedia page if false
        var EnableWikipediaSearch = true; 
        
        // sets whether the link to the original Wikipedia article should open in a new page        
        var LinkToWikipediaOpensInNewPage = true;
        
        // sets whether the Google ads have to be removed
        var RemoveAds = true;
        // END OF USER SETTINGS
        
        
        if(EnableWikipediaSearch){st=st[1].replace('+','%20')+''}
        else {st=st[1].replace('+','_')+''};
                   
        // taken from the original GooglePedia script
        var googleUrl = window.location.href.match(/http:\/\/(.*?)\//);
        googleUrl = 'http://'+googleUrl[1]+'/';

        var googleTld = window.location.href.match(/http:\/\/(.*?)google\.(.*?)\//);
        var googleTlds = { 'com':'en', 'co.uk':'en', 'co.jp':'ja', 'co.il':'he' };
        var wikiSubdomain = googleTlds[googleTld[2]];
        if(!wikiSubdomain) {wikiSubdomain = googleTld[2];}
        // -->
        
        //var strWiki='http://en.wikipedia.org/wiki/';
        var strWiki;
        if(EnableWikipediaSearch) {strWiki = 'http://'+wikiSubdomain+'.wikipedia.org/w/wiki.phtml?go=GO&search='}
        else {strWiki = 'http://'+wikiSubdomain+'.wikipedia.org/wiki/'};
        
        window.opera.addEventListener(
    'AfterEventListener.load',
    function(){
        // change results header
        var t=document.getElementsByTagName('table');
        if(!t)return;var i=0;
        while(t[i].getAttribute('bgcolor')!='#e5ecf9')i++;
        t=t[i];
        if(t.getElementsByTagName('td')[1].innerHTML.indexOf('WikiPedia')<0){
            t.getElementsByTagName('td')[0].appendChild(document.createElement('br'));
            t.getElementsByTagName('td')[0].appendChild(t.getElementsByTagName('td')[1].removeChild(t.getElementsByTagName('td')[1].childNodes[0]));
            t.getElementsByTagName('td')[0].style.padding = '0 0 4px 4px';
            t.getElementsByTagName('td')[0].childNodes[0].childNodes[0].nodeValue='';
            
            var tlink = document.createElement('a');
            tlink.setAttribute('onclick','javascript:document.getElementById("googlepedia_iframe").style.display=(document.getElementById("googlepedia_iframe").style.display=="block")?"none":"block"');
            tlink.appendChild(document.createTextNode('WikiPedia'));
            tlink.style.fontWeight='bold';
            tlink.style.fontSize='larger';
                    t.getElementsByTagName('td')[1].appendChild(tlink);
            
                    t.getElementsByTagName('td')[1].appendChild(document.createElement('br'));
                    var tspan = document.createElement('span');
                    tspan.style.fontSize='x-small';
                    tspan.appendChild(document.createTextNode('('));
                    tlink = document.createElement('a');
                    tlink.setAttribute('href',strWiki+st);
                    if(LinkToWikipediaOpensInNewPage) tlink.setAttribute('target','_blank');
                    tlink.appendChild(document.createTextNode('open'));
                    tspan.appendChild(tlink);
                    tspan.appendChild(document.createTextNode(')'));
                    t.getElementsByTagName('td')[1].appendChild(tspan);
                    t.getElementsByTagName('td')[1].style.padding = '0 4px 4px 0';
                    t=t.nextSibling;
            while(t.nodeType!=1)t=t.nextSibling;
        
            // add WikiPedia results
            var ifr=document.createElement('iframe');
            ifr.src=strWiki+st;
            ifr.setAttribute('id','googlepedia_iframe');
            if(wikiSubdomain=='he'){ifr.style='width:50%;height:'+HeightOfWikipediaPage+';float:left;border:solid 1px #aaa;margin:0 20px 20px 0';}
            else {ifr.style='width:50%;height:'+HeightOfWikipediaPage+';float:right;border:solid 1px #aaa;margin:0 0 20px 20px;';};
            ifr.style.display=(ShowWikipediaByDefault)?"block":"none";
            t.parentNode.insertBefore(ifr,t);
            }
            if(RemoveAds){
                var ads=document.getElementsByTagName('font');
                for (i=0,ai;ai=ads[i];i++)if(ai.className=='a'){ai.parentNode.parentNode.parentNode.parentNode.parentNode.style.display='none';}else{if(ai.getAttribute('color')=='#cc0000')ai.parentNode.parentNode.parentNode.parentNode.parentNode.style.display='none';}
        ads = document.getElementsByTagName('span');
                for (i=0,ai;ai=ads[i];i++)if(ai.className=='a')ai.parentNode.parentNode.style.display='none';
                }
    },false);
    }
}

if(window.location.href.indexOf('.wikipedia.org')>-1){
    window.opera.addEventListener(
        'BeforeScript',
        function(ev){
            // remove function that makes it impossible to open wikipedia in a frame and add function to hide unnecessary page elements
            if(ev.element.src.indexOf('.wikipedia.org/skins-1.5/common/wikibits.js?1')>-1)ev.element.text=ev.element.text.replace('window.top.location = window.location;','window.addEventListener("load",function(){var d=document.getElementById("content");d.style.margin="0 10px 0 0";d.style.border="none";d.style.fontSize="11px";d=document.getElementById("footer");d.style.display="none";d=document.getElementsByTagName("div");for(var i=0,di;di=d[i];i++)if(di.className=="portlet")di.style.display="none";},false);');},
        false);
}


Sunday, 18. June 2006, 13:34:25

UserJS.org candidate?

Monday, 19. June 2006, 22:03:17

GT500

Security Industry

avatar

Posts: 3450

Fighting Malware

Interesting, but in Opera 9 it just redirects to Wikipedia after loading the Google search results.

Monday, 19. June 2006, 22:44:50

shoust

Operaised

avatar

Posts: 3009

United Kingdom

Originally posted by scipio:

I don't think it's possible to create a UserScript that does exactly the same thing due to cross-domain restrictions. But here's a first try to get something similar. It still needs some improvements.



http://virtuelvis.com/archives/2005/12/cross-document-messaging Could make it possible, I used the method so the bugmenot bar i did could work.

Tuesday, 20. June 2006, 16:43:33

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by GT500:

Interesting, but in Opera 9 it just redirects to Wikipedia after loading the Google search results.


I'll take a look. Have to upgrade now anyway. :wink:

Originally posted by shoust:

http://virtuelvis.com/archives/2005/12/cross-document-messaging Could make it possible, I used the method so the bugmenot bar i did could work.


Thanks for the tip, I think I have missed that blog post when it was published.

Thursday, 22. June 2006, 18:06:55 (edited)

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by adamos:

didn't work link


I know that it doesn't work in Opera 9, I am working on it.

edit: Uhm... actually, it does work. You have to click on Wikipedia to make it visible, or change the script: the following line

var ShowWikipediaByDefault = false;

should be

var ShowWikipediaByDefault = true;

Thursday, 22. June 2006, 18:10:10

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by GT500:

Interesting, but in Opera 9 it just redirects to Wikipedia after loading the Google search results.


I can't confirm that... Maybe you didn't copy the entire script; the last part should prevent Wikipedia from "taking over" the Google screen.

Friday, 23. June 2006, 03:45:10 (edited)

GT500

Security Industry

avatar

Posts: 3450

Fighting Malware

Originally posted by scipio:


I can't confirm that... Maybe you didn't copy the entire script; the last part should prevent Wikipedia from "taking over" the Google screen.



I wonder if it's a Linux only issue. I haven't tried it in Windows yet. I just upgraded to the final build of Opera 9, which had the same build date and build number as the weekly build I had been using.

I changed the value for the variable 'ShowWikipediaByDefault' to true, and it looks like it does it as it's trying to load the Wikipedia content.

I'm going to give this a try in Windows. If I can get it to work there, I might file a bug report. Once upon a time, I had paid tech support (purchased Opera 7). If I can manage to get logged back in, I'll mention it to their tech support... :wink:


Never mind. It works fine...

I had set up the per-site preferences for Google.com to look for UserJS scripts in a certain folder, and then saved the googlepedia.js file in it. Opera wasn't allowing the small script at the end to affect Wikiepedia. I had to set per-site prefferences for Wikipedia.org to look in the same folder, so that it would grab the script as well.

Sorry for the confusion. I guess I caused my own problems there. But I guess it's worth mentioning that this script is best not used in conjunction with Opera's per-site preferences. Unless you're brave, like me, and feel like spliting the Wikipedia code off from the Google code, and dumping it in it's own directory... :lol:

Friday, 23. June 2006, 03:55:43

GT500

Security Industry

avatar

Posts: 3450

Fighting Malware

I'd like to publish this UserJS script on my website (GT500.org), if that's alright with you.

I'll probably make it a static page on my blog. If you want me to remove it, just say so. :wink:

Friday, 23. June 2006, 05:20:14

GT500

Security Industry

avatar

Posts: 3450

Fighting Malware

Static page is up, with instruction on how to install UserJS script.

I'm calling it GoogleWithPedia for now. I figured that calling it Googlepedia would just make the guys responsible for the Firefox thing mad, so I changed it slightly. If anyone has a better name, I'm open to sugestions.

If you want it removed from my site, or renamed, then just say so. :wink:

Saturday, 24. June 2006, 20:16:03 (edited)

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by GT500:

I had set up the per-site preferences for Google.com to look for UserJS...


p: I'm glad it works now.

Originally posted by GT500:

If you want it removed from my site, or renamed, then just say so.


It's fine with me, though a reference to the author and/or this thread would be nice. :wink: I'll be submitting the script to UserJS.org once they've solved their problems.

Btw, what's up with the last paragraph? :confused:

Saturday, 24. June 2006, 18:25:45

GT500

Security Industry

avatar

Posts: 3450

Fighting Malware

Originally posted by scipio:

It's fine with be, though a reference to the author and/or this thread would be nice. :wink: I'll be submitting the script to UserJS.org once they've solved their problems.

Btw, what's up with the last paragraph? :confused:



You'll get a refference. Should have thought about it in the first place, shouldn't I have?

The last paragraph was a warning to Firefox users to leave me alone. When Firefox 'fanboys', as I've seen them called (could be compaired to religious Zealots), find something that they don't like, or that doesn't properly promote Firefox's mythos, they often react by flaming, spamming, or various others acts of stupidity. I figured that some of those retarted little dorks who have nothing better to do would probably stumble upon that page eventually, and decide that my site should be their next target.

I thought it fair warning, as I have never lost an argument with a Firefox user. And I've had quite a few, and am familiar with most of their anti-Opera propaganda. :wink:

Maybe I'll make the warning a seperate page, and just link to it in the paragraph where I mention the fact that the idea did come from the Firefox addon.

Tuesday, 4. July 2006, 02:25:51

Why can't I get the Wikipedia page to open up in a frame; it just opens up a new window.

I assume I am overlooking something obvious !

Tuesday, 4. July 2006, 06:20:03

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by nightfishing:

Why can't I get the Wikipedia page to open up in a frame; it just opens up a new window.


Did you get the latest version of the script? Which Google page do you use, Google.com? The Wiki page is "protected" against being opened in a frame. The last part of the script (starting with if(window.location.href.indexOf('.wikipedia.org')>-1) and ending at false);} removes that protection.

Friday, 7. July 2006, 03:17:41

GT500

Security Industry

avatar

Posts: 3450

Fighting Malware

Originally posted by nightfishing:

Why can't I get the Wikipedia page to open up in a frame; it just opens up a new window.

I assume I am overlooking something obvious !



If you are using Opera 9's per-site preferences, then you need to set up wikipedia.org and google.com to look in the same directory for User JavaScript, as the script overides things on Wikipedia's page as well as Google's, and Opera won't automatically apply it to both... :wink:

Saturday, 8. July 2006, 03:56:17

Stoen

avatar

Posts: 1109

Nice script but the code you use for localised domains needs to be changed a bit. Take google.com.au for an example. Perhaps add a default setting for this or change
if(!wikiSubdomain) {wikiSubdomain = googleTld[2];}

to
if(!wikiSubdomain) {wikiSubdomain = googleTlds['com'];}

Saturday, 8. July 2006, 18:28:24 (edited)

scipio

Undutchable

avatar

Posts: 29747

Netherlands

Originally posted by Stoen:

Nice script but the code you use for localised domains needs to be changed a bit. Take google.com.au for an example. Perhaps add a default setting for this or change

if(!wikiSubdomain) {wikiSubdomain = googleTld[2];}

to
if(!wikiSubdomain) {wikiSubdomain = googleTlds['com'];}


I think the code you suggested would break more than it would fix (for example for google.nl, google.fr, google.it, google.de, google.es etc.), but I understand what you mean. The script now contains all domains used by Google and links them to the most suitable WikiPedia language. For some multilingual countries I had to choose one of the official languages, which is why I felt the need to add a variable that lets the user override the language autodetection:
var DefaultWikiLanguage = '';
If left empty, the script will try to find the right Wikipedia language.

To take into account that there are several RTL languages in which it would be nicer to display the Wikipedia results on the left side of the page (like until now was done for Hebrew only), I've added another variable:
var GoogleLeftWikipediaRight = true;
Those who prefer Wikipedia on the left, should change this to False.

Moreover, this new version also contains a fix for some searches that would make the Google search results be displayed below the Wikipedia page.

Here's the script as a .txt file. Save it in your User Scripts folder and change the extension to .js.

PS - This is version 1.0.
PPS - Rijk called this script Operapedia, so I'll stick with that.

Operapedia 1.0

Saturday, 8. July 2006, 18:12:29

Congratulations. :smile:

You say that for Hebrew (google.co.il), the results display on the left, however, the wiki page shows on the right... error.

Forums » Opera Community » General Opera topics » User JavaScript