简体   繁体   English

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

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

A bookmarklet is a bookmark whose address is JavaScript code. 小书签是地址为JavaScript代码的书签。

I would like to get the URL of the current page I am on and paste that into the text box of the Bing search page. 我想获取当前页面的URL,并将其粘贴到Bing搜索页面的文本框中。

I can get the URL easily enough: 我可以很容易地获得URL:

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

But then how do I set the text box on the Bing page to my variable, url and then make it search? 但是,如何将Bing页面上的文本框设置为变量url ,然后进行搜索呢?

This does not work: 这不起作用:

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)}

Of course you can do the way you have seen above. 当然,您可以按照上面的方式进行操作。 However, I have been in this situation where I wanted to control what to show from within my application. 但是,在这种情况下,我想控制从应用程序中显示的内容。

Then I decided to connect my application from Bing API. 然后,我决定从Bing API连接我的应用程序。 The benefit is that it is free and you will not take user away from your website. 好处是它是免费的,您不会使用户离开您的网站。

You will need to get the API Key from the Azure Market Place 您将需要从Azure市场获得API密钥

Here is the code that you might want to give it a try , may be, in the future. 这是您将来可能想要尝试的代码。

<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