简体   繁体   English

添加书签此页按钮 - 2016年

[英]Add bookmark this page button - 2016

I've seen this question already multiple times: How to add a bookmark this page button. 我已经多次看过这个问题:如何在此页面按钮添加书签。 But it seems no solution is working currently. 但似乎目前还没有解决方案。

The code im trying to use at the moment: 我现在试图使用的代码:

$('.js-bookmarkme').click(function(e) {
    e.preventDefault();

    if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(document.title,window.location.href,'');
    } else if(window.external && ('AddFavorite' in window.external)) { // IE Favorite
        window.external.AddFavorite(location.href,document.title);
    } else if(window.opera && window.print) { // Opera Hotlist
        this.title=document.title;
        return true;
    } else { // webkit - safari/chrome
        alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }
});

Source: How do I add an “Add to Favorites” button or link on my website? 来源: 如何在我的网站上添加“添加到收藏夹”按钮或链接?

As stated in the comments: 如评论中所述:

Firefox's propriety window.sidebar.addPanel(..) has been deprecated, and the function was removed in Firefox 23 (see third bullet) Firefox的专有window.sidebar.addPanel(..)已被弃用, 该功能已在Firefox 23中删除 (参见第三个子弹)
– Will Hawker - 霍尔克尔

Supposedly the FF solution to date isn't working anymore, but the Opera solution isn't working either. 据说迄今为止FF解决方案已不再适用,但Opera解决方案也无法正常工作。 (Though I wasn't able to test the IE solution yet). (虽然我还没能测试IE解决方案)。

That brings in the obvious question: How can you achive a Bookmarklet button? 这带来了一个显而易见的问题:你如何获得Bookmarklet按钮? With browser support as far as possible. 尽可能使用浏览器。

Since there has been no solution this is the best I was able to come up with, after some research. 由于没有解决方案,经过一些研究,这是我能够想到的最好的解决方案。

// Bookmark me
$('.js-bookmarkme').click(function(e) {
    e.preventDefault();
    var bookmarkURL = window.location.href;
    var bookmarkTitle = document.title;

    if ('addToHomescreen' in window && window.addToHomescreen.isCompatible) {
        // Mobile browsers
        addToHomescreen({ autostart: false, startDelay: 0 }).show(true);
    } else if (window.sidebar && window.sidebar.addPanel) {
        // Firefox version < 23
        window.sidebar.addPanel(bookmarkTitle, bookmarkURL, '');
    } else if ((window.sidebar && /Firefox/i.test(navigator.userAgent)) || (window.opera && window.print)) {
        // Firefox version >= 23 and Opera Hotlist
        $(this).attr({
            href: bookmarkURL,
            title: bookmarkTitle,
            rel: 'sidebar'
        }).off(e);
        return true;
    } else if (window.external && ('AddFavorite' in window.external)) {
        // IE Favorite
        window.external.AddFavorite(bookmarkURL, bookmarkTitle);
    } else {
        // Other browsers (mainly WebKit - Chrome/Safari)
        alert('Please press ' + (/Mac/i.test(navigator.userAgent) ? 'CMD' : 'Strg') + ' + D to add this page to your favorites.');
    }

    return false;
});

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

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