代码之家  ›  专栏  ›  技术社区  ›  Tyler Carter

使用jQuery选择已访问的链接

  •  8
  • Tyler Carter  · 技术社区  · 16 年前

    我正试图通过jQuery选择所有访问的链接。这是HTML

    <div class="question-summary">
        <a class="question-hyperlink">Stuff</a>
    </div>
    

    question-hyperlink question-summary . 有什么想法吗?

    4 回复  |  直到 16 年前
        1
  •  41
  •   panepeter    6 年前

    给出的方法已经过验证 出于安全原因而禁用 .

    由于可以通过检查访问的链接来检索访问者的历史记录,浏览器供应商已经采取了某些措施来防止这种情况。

    资料来源: Mozilla Foundation Blog.

    签入Chrome和FF-两者都不支持 $("a:visited") 再也不会了。

        2
  •  3
  •   Maciej    11 年前

    我在上找到了基于本地存储的解决方法 Nevyan's Blog: Mark visited links using JavaScript and localStorage

    <a> 要素:

    function check_visited_links() {
        var visited_links = JSON.parse(localStorage.getItem('visited_links')) || [];
        var links = document.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++) {
            var that = links[i];
            that.onclick = function() {
                var clicked_url = this.href;
                if (visited_links.indexOf(clicked_url) == -1) {
                    visited_links.push(clicked_url);
                    localStorage.setItem('visited_links', JSON.stringify(visited_links));
                }
            }
            if (visited_links.indexOf(that.href) !== -1) {
                that.parentNode.className += ' visited';
            }
        }
    }
    

    不过,我不知道这是否比“拜访法”更安全。

        3
  •  0
  •   pendave    9 年前

    在这里输入代码` javascript不支持它,因为我还试图找到方法来收集:访问的链接数据,以隐藏访问的节点。

    如果你所关心的只是样式,你应该能够通过CSS实现它,但是通过屏幕上显示的内容应该是观察它被访问的唯一方式。

    我在Greasemonkey的用户脚本中这样做,让那些没有:visited样式的站点显示那些已经访问过的链接。

    // ==UserScript==
    // @description    ADD a:visited for CSS
    // @include        *annalscts.com*
    // @include        *thejns.org*
    // @include        *turkishneurosurgery.org.tr*
    // @include        *nature.com*
    // @include        *academic.oup.com*
    // @include        *sagepub.com*
    // @grant          GM_addStyle
    // ==/UserScript==
    GM_addStyle('a:visited {color:#EE5665 !important}');
    

    为了将数据收集到本地,我使用Greasemonkey API

    GM_setValue 
    GM_getValue
    

    我刚刚在Youtube上观看了API的教程,并尝试写入用户脚本

    Greasemonkey API:Values只需在Youtube上搜索此标题。

    http://nulleffort.com/greasemonkey-api-values/

    Greasemonkey文档: https://wiki.greasespot.net/Greasemonkey_Manual:API

    //Firstly, var the ordinary variable preVisitedLinks and assigning to memory variable visitedLinks (At first the value should be undefined)
    var preVisitedLinks = GM_getValue("visitedLinks");
    unsafeWindow.aclick = function(tlink){
        window.open(tlink, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,top=10,left=10,width=10,height=10'); // click a button added and get the link visited in my script 
        //If the ordinary variable preVisitedLinks is undefined (First time running the script)
        if(preVisitedLinks.includes('undefined')){
            GM_setValue('preVisitedLinks', '|' + tlink.replace('http://paper.pubmed.cn/',''));
        }
        //If the ordinary variable preVisitedLinks is not undefined, continue to add each new string collect
        else{
            GM_setValue('preVisitedLinks', preVisitedLinks + '|' + tlink.replace('http://paper.pubmed.cn/',''));
        }
        //The ordinary variable preVisitedLinks assigning to memory variable visitedLinks value. The magic is the variable name the same.
        preVisitedLinks = GM_getValue("preVisitedLinks");
        if(preVisitedLinks.length > 27500){
            preVisitedLinks = preVisitedLinks.substr(preVisitedLinks.length - 27500);
        }
        //The memory variable visitedLinks value assigning to the ordinary variable preVisitedLinks value
        GM_setValue('visitedLinks',preVisitedLinks);
        console.info(preVisitedLinks);
    };
    

    在某些地方,我使用字符串来检测访问的链接代码

    if(preVisitedLinks.includes(trs[i].querySelectorAll('li')[0].querySelector('a').href.replace('http://xxx.xxxx.com/',''))){
            trs[i].remove();
        }