繁体   English   中英

Javascript小书签可将当前页面的网址发送到bing

[英]Javascript bookmarklet to send url of current page to bing

小书签是地址为JavaScript代码的书签。

我想获取当前页面的URL,并将其粘贴到Bing搜索页面的文本框中。

我可以很容易地获得URL:

javascript:(function(){var%20url=window.location.href;alert(url);})();

但是,如何将Bing页面上的文本框设置为变量url ,然后进行搜索呢?

这不起作用:

javascript:(function(){var%20url=window.location.href;window.open%20("https://www.bing.com/search?q=&url");})();

使用以下书签代码:

javascript:{window.location='http://bing.com/search?q='+encodeURIComponent(window.location.href)}

当然,您可以按照上面的方式进行操作。 但是,在这种情况下,我想控制从应用程序中显示的内容。

然后,我决定从Bing API连接我的应用程序。 好处是它是免费的,您不会使用户离开您的网站。

您将需要从Azure市场获得API密钥

这是您将来可能想要尝试的代码。

<html>
<head>
<title>BING API Integration</title>
<SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">


$(function() {
    $('#searchButton').click(function(e){
        $("#results").empty();
        var query=$('#searchTerm').val();
        if ( query) {
            serviceOp = "Web";
            search(query, serviceOp);
        }
    });
});

function search(query, serviceOp){


    // your account key that youw ill get from https://datamarket.azure.com
    var acctKey = '<Your Key>';

    var rootUri = 'https://api.datamarket.azure.com/Bing/Search';

    var requestUri = rootUri + "/" + serviceOp + "?$format=json&Query='" + query + "'";


    $.ajax({
        type: "GET",
        url: requestUri,
        headers: {
        "Authorization": "Basic " + window.btoa(acctKey  + ":" + acctKey)
        },

    }).done(function(o){

        if ( o.d !== undefined){
                var items = o.d.results;
                    for(var idx=0, len= items.length; idx < len; idx++ ){
                        var item = items[idx];
                        switch(item.__metadata.type){
                            case 'WebResult':
                            showWebResult(item);
                        }
                    }
                }
    });
}

// Shows one item of Web result.

function showWebResult(item) {

    var p = document.createElement('p');
    var a = document.createElement('a');
    a.href = item.Url;
    $(a).append(item.Title);
    $(p).append(item.Description);
    $('#results').append(a, p);
}
</script>
</head>
<body>
    <label for="searchTerm">Search: </label>
    <input id="searchTerm" type="text"/>
    <button id="searchButton">Search</button>

    <div id="results">
    </div>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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