如何突出Google搜索结果中的特定域名?

时间:2014-09-10 02:13:34

标签: javascript firefox-addon greasemonkey

下面的greasemonkey脚本突出显示已定义的域。 例如:我的搜索查询是书,我的域名是亚马逊。如果此域的结果多于零,则会突出显示div。

但它不起作用。因为它是为大约四年前的旧版Google设计而设计的。我尝试改变一些东西,但没有。

脚本来源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);
                }
            }
        }
}

我在脚本的这一部分改变了一些东西,但仍无法正常工作

    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++;
                    }
        }

1 个答案:

答案 0 :(得分:3)

您将要使用JQuery。如果我理解正确的话,这是一个非常简单的脚本。注意:Google安装的jQuery无法正常运行,所以无论你做什么,你都需要自己的。

// ==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);
}

功能齐全的工作脚本。

相关问题