简体   繁体   中英

How to highlight specific domain in Google search results?

Below greasemonkey script highlight defined domains. Ex: my search query is book and my domain is amazon. If there is more than zero results for this domain, it highlights that div.

But it is not working. Because it designed for old Google design about four years ago. I try to change something but nothing.

Script source : Locate Multiple Domains Highlights

// ==UserScript==
// @name           Locate Multiple Domains in Search Results
// @filename       multiple-domains.user.js
// @author         Alex Hall
// @homepage       http://www.devseo.co.uk
// @namespace      http://www.devseo.co.uk/blog/view/greasemonkey-script
// @version        1.0
// @description    Locate specific domains easily within the results of a Google, Yahoo!, or Bing Search
// @include        http://www.google.com/search*
// @include        http://google.com/search*
// @include        http://www.google.com/#*
// @include        http://google.com/#*
// ==/UserScript==

//***********************BEGIN CONFIGURATION**********************//
// Set the array of sites you wish to locate (without www is best) - syntax [['mydomain.com', '#color'], ['mydomain2.com', '#color']];
// Set the color variable to the background color you wish to use to highlight your domain (defaults to light red)
var sites = [
        ['domainxyz.com', '#FFCC66'],
        ['www.domainxyz.com', '#C2D9EF'],
];

var siteLen = sites.length;
var numNot = 0;

//************************END CONFIGURATION***********************//

//*******************DO NOT EDIT PAST THIS LINE*******************//
//****************UNLESS YOU KNOW WHAT YOU'RE DOING***************//
setTimeout(startItOff, 1000);
function startItOff()
{
        var all_links = document.getElementsByTagName('cite');
        var linkLen = all_links.length;
        if (location.hostname.indexOf("google.com")!=-1) {
            for (i=0; i<linkLen; i++) {
                if (all_links[i].className!='l') { continue; }
                var href = all_links[i].value;
                        if(all_links[i].parentNode.tagName.toLowerCase() === 'div'){
                                numNot++;
                        }
            }
            for (i=0; i<linkLen; i++) {
                if (all_links[i].className!='l') { continue; }
                var href = all_links[i].getAttribute('href');

                for(j=0;j<siteLen;j++){
                   if (href.indexOf(sites[j][0])==-1) { continue; }
                   var result=all_links[i].parentNode.parentNode;
                   result.style.backgroundColor=sites[j][1];
                   result.style.overflow='hidden';
                   result.style.width='740px';

                           var listingNum = all_links[i].getAttribute('onmousedown');
                           listingNum = listingNum.split(',');
                           listingNum = listingNum[4].split('\'');
                           listingNum = listingNum[1];
                           var resultDiv = document.createElement('div');
                           result.appendChild(resultDiv);
                           resultDiv.style.cssText = 'float: right;font-size:16px;margin:0 5px 5px 0;background: #FFF;padding: 5px;';
                           resultDiv.innerHTML = 'Result ' + (listingNum - numNot);
                }
            }
        }
}

I changed something on this part of script, still it is not working

    var all_links = document.getElementsByTagName('cite');
    var linkLen = all_links.length;
    if (location.hostname.indexOf("google.com")!=-1) {
        for (i=0; i<linkLen; i++) {
            if (all_links[i].className!='l') { continue; }
            var href = all_links[i].value;
                    if(all_links[i].parentNode.tagName.toLowerCase() === 'div'){
                            numNot++;
                    }
        }

You are going to want to use use JQuery. And it is a very simple script, if I understand correctly. Note: the jQuery Google has installed does not work properly, so you definitely need your own, no matter what you do.

// ==UserScript==
// @name           Google Search Domain Highlighter
// @author         Wisnoskij
// @homepage       https://openuserjs.org/users/wisnoskij
// @namespace      https://openuserjs.org/users/wisnoskij
// @version        1.0
// @description    Locate specific domains easily within the results of a Google Search
// @include        /https?://(www\.)?google\.[^/]*/search[^/]*/
// @require        http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==
var sites = {
    'stackoverflow.com': '#FFCC66',
    'tutorialspoint.com': '#C2D9EF',
    'css-tricks.com': '#CCFF66'
};
var numHL = 0;

$('._Rm').each(highlight);
function highlight(index, element){
    try{
    var node = $(element);
    var site = node.text();

    site = getDomain(site);
    if(sites[site]){
        node.closest('.rc').css('background-color', sites[site]);
        return;
    }
    site = removeWWW(site);
    if(sites[site]){
        node.closest('.rc').css('background-color', sites[site]);
        return;
    }
    }catch(er){}
 }

function getDomain(url){
    var tmp = url.indexOf('://');
    if(tmp > -1){
        url = url.substring(tmp+3);
    }
    url = url.match(/^[^/]*/)[0];
    tmp = url.indexOf(' › ');
    if(tmp > -1){
        url = url.substring(0, tmp);
    }
    return(url);
}
function removeWWW(url){
    var tmp = url.indexOf('.');
    if(tmp > -1){
        url = url.substring(tmp+1);
    }
    return(url);
}

Fully functional, working script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM