2

是否可以在 Google 自定义搜索的页面结果中添加下一个和上一个链接?我找到了这个线程如何在 Google 自定义搜索引擎分页链接中显示下一个/上一个链接,但代码适用于旧版本的 Google 自定义搜索。

较新的版本只是给你一个类似的标签<gcse:searchresults-only></gcse:searchresults-only>,一切都由此生成。有任何想法吗?谢谢

这就是搜索结果被拉进来的方式。

<script>
(function() {
    var cx = 'xxxxxx:xxxxx',
        gcse = document.createElement('script');

    gcse.type = 'text/javascript'; gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>

<gcse:searchresults-only></gcse:searchresults-only>
4

2 回答 2

0

你可以使用searcher.cursor.currentPageIndexsearcher.gotoPage()所有这些包裹在google.setOnLoadCallback

window.customSearchControl = customSearchControl;

$(function() {

    var link = $('<a />', {
        'html' : 'link',
        'href' : '#',
        'class' : 'general-button',
        'style' : 'margin-right:1em'
    });

    var searcher = window.customSearchControl.getWebSearcher();

    var prev = link.clone().click(function(e) {
        e.preventDefault();
        var topage = searcher.cursor.currentPageIndex - 1;
        if(topage < 1) {
            topage = 0;
        }
        searcher.gotoPage(topage);
        return false;
    }).html('&lt; prev');

    var next = link.clone().click(function(e) {
        e.preventDefault();
        var topage = searcher.cursor.currentPageIndex + 1;
        if(topage > searcher.cursor.pages.length) {
            topage = 10;
        }
        searcher.gotoPage(topage);
        return false;
    }).html('next &gt;');

    $('#cse').append(prev).append(next);
});
于 2014-05-09T04:24:45.420 回答
0

我使用了一些 Jquery,但可以不用。我把解释放在代码中。

 //Listens for DOM changes
var observer = new MutationObserver(function (mutations, observer) {
    // fired when a mutation occurs
    makeNext();
});

// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document, {
    subtree: true,
    attributes: true
});

function makeNext() {
    //Make a Next element
    var el = $("<div></div>", {
        "id": "next",
        "class": "gsc-cursor-page", // need google class so is same look and works with them
        "text": "Next",
        "on": { // on click, find what is the current page, and trigger click on the one after
            "click": function () {
                $(".gsc-cursor-current-page").next(".gsc-cursor-page").click();
            }
        }
    });
    //When results load, lots of changes are made, but we only wont 1 Next button
    if ($("#next").length == 0) {
        $(".gsc-cursor").append(el);
    }
}
于 2019-11-05T12:54:23.247 回答