【问题标题】:How to access the visited links history如何访问访问过的链接历史
【发布时间】:2016-02-16 04:39:56
【问题描述】:

我曾尝试使用 CSS :visited 属性获取访问过的链接,但它不起作用。

是否有任何方法或解决方法可以使用 JavaScript、jQuery 或通过任何其他技术获取访问的链接?

【问题讨论】:

  • 即使您无法访问用户的历史记录,您也只能使用 JavaScript 检查用户通过 document.referrer 访问的最后一页是什么。

标签: javascript jquery css visited


【解决方案1】:

enter code herejavascript 不支持,因为我也在尝试寻找方法来收集 a:visited links 数据以隐藏访问过的节点。

一些参考: 隐私和 :visited 选择器 - CSS | MDN

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

我在 Greasemonkey 的用户脚本中这样做是为了让那些没有 a: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:值只是在 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();
    }

【讨论】:

    【解决方案2】:

    您可以使用localStorage 创建自己的链接历史记录,并存储点击了哪些链接以及点击了多少次。

    Stack Snippet 中的这段代码可能无法在下面运行,但您可以将代码粘贴到您的文件或 JSFiddle 中,它会正常运行。链接历史会保存在localStorage,每次点击锚点对应的计数都会增加。

    $('a').on("click",function(){
    var anchorhistory=localStorage.getItem($(this).attr("id"));
    if(anchorhistory){
    anchorhistory=parseInt(anchorhistory)+1;
    localStorage.setItem($(this).attr("id"), anchorhistory);
    alert($(this).attr("id")+" is clicked "+anchorhistory+" Times");
    }
    else{
    anchorhistory=1;
    localStorage.setItem($(this).attr("id"), anchorhistory);
    alert($(this).attr("id")+" is clicked "+anchorhistory+" Times");
    
    }
    })
    ul a{
      cursor:pointer;
      color:blue;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul>
    <li><a id='anchor1'>anchor1</a></li>
    <li><a id='anchor2'>anchor2</a></li>
    <li><a id='anchor3'>anchor3</a></li>
    <li><a id='anchor4'>anchor4</a></li>
    </ul>

    【讨论】:

      【解决方案3】:

      正如@Nevershowmyface 所说,您可以只访问上次访问的页面。

      这里有两个例子,one using CSSone using jQuery

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 2011-04-02
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多