Javascript 如何在页面上检测已访问和未访问的链接?

Javascript 如何在页面上检测已访问和未访问的链接?,javascript,firefox,hyperlink,click,greasemonkey,Javascript,Firefox,Hyperlink,Click,Greasemonkey,我的目标是检测网页上未访问的链接,然后创建greasemonkey脚本来单击这些链接。这里的未访问链接是指未被我打开的链接。由于我可以看到所有浏览器都提供了更改已访问和未访问链接颜色的功能,因此有可能以任何方式检测这些链接。 在搜索时,我发现了这个链接:但这里有人告诉我,这已经不可能了。请提供帮助。正确,javascript无法检测是否在Firefox或Chrome中访问了链接,这是此Greasemonkey上下文中唯一适用的两种浏览器 这是因为Firefox和Chrome非常重视安全和隐私。发

我的目标是检测网页上未访问的链接,然后创建greasemonkey脚本来单击这些链接。这里的未访问链接是指未被我打开的链接。由于我可以看到所有浏览器都提供了更改已访问和未访问链接颜色的功能,因此有可能以任何方式检测这些链接。
在搜索时,我发现了这个链接:但这里有人告诉我,这已经不可能了。请提供帮助。

正确,javascript无法检测是否在Firefox或Chrome中访问了链接,这是此Greasemonkey上下文中唯一适用的两种浏览器

这是因为Firefox和Chrome非常重视安全和隐私。发件人:

注意。样式表作者可能滥用:link和:visted伪类来确定用户未经用户同意访问了哪些站点

因此,UAs可以将所有链接视为未访问的链接,或者实施其他措施来保护用户的隐私,同时以不同方式呈现已访问和未访问的链接。有关处理隐私的更多信息,请参见[P3P]

另请参见,
您可以看到一个演示,它显示安全的ish浏览器不允许您嗅探访问的链接




对于您的特定情况,因为您正在访问一个页面并保持其打开状态,所以您可以帮助脚本跟踪访问了哪些链接。这不是傻瓜式的,但我相信它会满足你的要求

首先,通过执行以下操作查看正在运行的脚本:

  • 按原样安装脚本
  • 浏览到测试页面,。
    每次重新加载或刷新时,测试页面都会添加一个新链接
  • GM脚本在其运行的页面上添加了2个按钮。左上角有一个“启动/停止”按钮,右下角有一个“清除”按钮

    当您按下“开始”按钮时,它会执行以下操作:

  • 页面上的所有现有链接都记录为“已访问”
  • 它启动一个计时器(默认设置:3秒),当计时器关闭时,它将重新加载页面
  • 每次页面重新加载时,它都会打开任何新链接并启动新的重新加载计时器
  • 按“停止”按钮停止重新加载,访问的链接列表将保留

  • “清除”按钮将删除已访问页面的列表。
    警告:如果在刷新循环处于活动状态时按“清除”,则下次重新加载页面时,所有链接将在新选项卡中打开


    接下来,在您的站点上使用脚本

    仔细阅读脚本中的注释,您必须更改
    @include
    @exclude
    selectorStr
    值以匹配您正在使用的站点

    为获得最佳效果,禁用任何“每次重新加载”加载项或“自动更新”选项。


    重要提示:
  • 脚本必须使用永久存储来跟踪链接。
    选项有:cookies、、和

    这些都有缺点,在这种情况下(单个站点,潜在的大量链接,多个会话),
    localStorage
    是最好的选择(
    IndexedDB
    可能是,但它仍然太不稳定--导致我的机器上频繁发生FF崩溃)

    这意味着只能对每个站点的链接进行跟踪,“安全”、“隐私”或“更干净”的实用程序可以阻止或删除访问的链接列表。(就像清除浏览器的历史记录一样,将重置已访问链接的任何CSS样式。)

  • 目前,该脚本仅适用于Firefox。它不应该在Chrome上工作,即使安装了Tampermonkey,也需要进行一些重新设计



  • 剧本:
    /*******************************************************************************
    **此脚本:
    **1)跟踪已单击的链接。
    **2)定期刷新页面以检查新链接。
    **3)如果找到新链接,则在新选项卡中打开这些链接。
    **
    **设立:
    **1)根据具体情况仔细选择并指定“selectorStr”
    **目标页面的。
    **选择器字符串使用任何有效的jQuery语法。
    **2)将@include和/或@exclude和/或@match指令设置为
    **适用于目标站点。
    **3)关闭任何“自动更新”功能。同样,不要使用任何
    **“重新加载每个”插件。此脚本将处理重新加载/刷新。
    **
    **使用:
    **该脚本将在页面上放置2个按钮:中的“开始/停止”按钮
    **左上角和左下角的“清除”按钮。
    **
    **按“开始”按钮开始脚本重新加载页面,然后
    **打开任何新链接。
    **当按下按钮时,假定任何现有链接都已存在
    **有人参观过。
    **
    **按下“停止”按钮以停止重新加载和链接打开。
    **
    **“清除”按钮会删除已访问链接的列表,这可能会
    **否则将永远保存。
    **
    **方法:
    **使用localStorage跟踪状态机状态,并保留
    **访问链接的持久列表。
    **
    **使用jQuery和一些GM_u函数实现。
    **
    **目前,该脚本仅适用于Firefox。它可能不会对你起作用
    **铬合金,即使有篡改。
    */
    //==用户脚本==
    //@name\u新链接/访问链接、跟踪器和开启器
    //@包括http://jsbin.com/*
    //@exclude/\/edit\b/
    //@需要http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    //@grant GM_addStyle
    //==/UserScript==
    /*-需要@grant指令来解决设计变更
    在GM 1.0中引入。它会恢复沙箱。
    */
    //---关键控制/设置变量:
    变量刷新延迟=3000;//--毫秒。
    var selectorStr='ul.topicList a.topicTitle';
    //--
    
    /*******************************************************************************
    **  This script:
    **      1)  Keeps track of which links have been clicked.
    **      2)  Refreshes the page at regular intervals to check for new links.
    **      3)  If new links are found, opens those links in a new tab.
    **
    **  To Set Up:
    **      1)  Carefully choose and specify `selectorStr` based on the particulars
    **          of the target page(s).
    **          The selector string uses any valid jQuery syntax.
    **      2)  Set the @include, and/or, @exclude, and/or @match directives as
    **          appropriate for the target site.
    **      3)  Turn any "Auto update" features off.  Likewise, do not use any
    **          "Reload Every" addons.  This script will handle reloads/refreshes.
    **
    **  To Use:
    **      The script will place 2 buttons on the page: A "Start/Stop" button in
    **      the upper left and a "Clear" button in the lower left.
    **
    **      Press the "Start" button to start the script reloading the page and
    **      opening any new links.
    **      When the button is pressed, it is assumed that any existing links have
    **      been visited.
    **
    **      Press the "Stop" button to halt the reloading and link opening.
    **
    **      The "Clear" button erases the list of visited links -- which might
    **      otherwise be stored forever.
    **
    **  Methodology:
    **      Uses localStorage to track state-machine state, and to keep a
    **      persistent list of visited links.
    **
    **      Implemented with jQuery and some GM_ functions.
    **
    **      For now, this script is Firefox-only.  It probably will not work on
    **      Chrome, even with Tampermonkey.
    */
    // ==UserScript==
    // @name        _New link / visited link, tracker and opener
    // @include     http://jsbin.com/*
    // @exclude     /\/edit\b/
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // @grant       GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    //--- Key control/setup variables:
    var refreshDelay    = 3000;    //-- milliseconds.
    var selectorStr     = 'ul.topicList a.topicTitle';
    
    //--- Add the control buttons.
    $("body")  .append (  '<div id="GM_StartStopBtn" class="GM_ControlWrap">'
                        + '<button>Start checking for new links.</button></div>'
                )
               .append (  '<div id="GM_ClearVisitListBtn" class="GM_ControlWrap">'
                        + '<button>Clear the list of visited links.</button></div>'
                );
    $('div.GM_ControlWrap').hover (
        function () { $(this).stop (true, false).fadeTo ( 50, 1); },
        function () { $(this).stop (true, false).fadeTo (900, 0.8); }// Coordinate with CSS.
    );
    
    //--- Initialize the link-handler object, but wait until the load event.
    var stateMachine;
    window.addEventListener ("load", function () {
            stateMachine    = new GM_LinkTrack (    selectorStr,
                                                    '#GM_StartStopBtn button',
                                                    '#GM_ClearVisitListBtn button',
                                                    refreshDelay
                                                );
    
            /*--- Display the current number of visited links.
                We only update once per page load here.
            */
            var numLinks    = stateMachine.GetVisitedLinkCount ();
            $("body").append ('<p>The page opened with ' + numLinks + ' visited links.</p>');
        },
        false
    );
    
    
    /*--- The link and state tracker object.
        Public methods:
            OpenAllNewLinks ()
            StartStopBtnHandler ()
            ClearVisitedLinkList ()
            StartRefreshTimer ();
            StopRefreshTimer ();
            SetAllCurrentLinksToVisited ()
            GetVisitedLinkCount ()
    */
    function GM_LinkTrack (selectorStr, startBtnSel, clearBtnSel, refreshDelay)
    {
        var visitedLinkArry = [];
        var numVisitedLinks = 0;
        var refreshTimer    = null;
        var startTxt        = 'Start checking for new links.';
        var stopTxt         = 'Stop checking links and reloading.';
    
        //--- Get visited link-list from storage.
        for (var J = localStorage.length - 1;  J >= 0;  --J) {
            var itemName    = localStorage.key (J);
    
            if (/^Visited_\d+$/i.test (itemName) ) {
                visitedLinkArry.push (localStorage[itemName] );
                numVisitedLinks++;
            }
        }
    
        function LinkIsNew (href) {
            /*--- If the link is new, adds it to the list and returns true.
                Otherwise returns false.
            */
            if (visitedLinkArry.indexOf (href) == -1) {
                visitedLinkArry.push (href);
    
                var itemName    = 'Visited_' + numVisitedLinks;
                localStorage.setItem (itemName, href);
                numVisitedLinks++;
    
                return true;
            }
            return false;
        }
    
        //--- For each new link, open it in a separate tab.
        this.OpenAllNewLinks        = function ()
        {
            $(selectorStr).each ( function () {
    
                if (LinkIsNew (this.href) ) {
                    GM_openInTab (this.href);
                }
            } );
        };
    
        this.StartRefreshTimer      = function () {
            if (typeof refreshTimer != "number") {
                refreshTimer        = setTimeout ( function() {
                                            window.location.reload ();
                                        },
                                        refreshDelay
                                    );
            }
        };
    
        this.StopRefreshTimer       = function () {
            if (typeof refreshTimer == "number") {
                clearTimeout (refreshTimer);
                refreshTimer        = null;
            }
        };
    
        this.SetAllCurrentLinksToVisited = function () {
            $(selectorStr).each ( function () {
                LinkIsNew (this.href);
            } );
        };
    
        this.GetVisitedLinkCount = function () {
            return numVisitedLinks;
        };
    
        var context = this; //-- This seems clearer than using `.bind(this)`.
        this.StartStopBtnHandler    = function (zEvent) {
            if (inRefreshCycle) {
                //--- "Stop" pressed.  Stop searching for new links.
                $(startBtnSel).text (startTxt);
                context.StopRefreshTimer ();
                localStorage.setItem ('inRefreshCycle', '0'); //Set false.
            }
            else {
                //--- "Start" pressed.  Start searching for new links.
                $(startBtnSel).text (stopTxt);
                localStorage.setItem ('inRefreshCycle', '1'); //Set true.
    
                context.SetAllCurrentLinksToVisited ();
                context.StartRefreshTimer ();
            }
            inRefreshCycle  ^= true;    //-- Toggle value.
        };
    
        this.ClearVisitedLinkList   = function (zEvent) {
            numVisitedLinks = 0;
    
            for (var J = localStorage.length - 1;  J >= 0;  --J) {
                var itemName    = localStorage.key (J);
    
                if (/^Visited_\d+$/i.test (itemName) ) {
                    localStorage.removeItem (itemName);
                }
            }
        };
    
        //--- Activate the buttons.
        $(startBtnSel).click (this.StartStopBtnHandler);
        $(clearBtnSel).click (this.ClearVisitedLinkList);
    
        //--- Determine state.  Are we running the refresh cycle now?
        var inRefreshCycle  = parseInt (localStorage.inRefreshCycle, 10)  ||  0;
        if (inRefreshCycle) {
            $(startBtnSel).text (stopTxt); //-- Change the btn lable to "Stop".
            this.OpenAllNewLinks ();
            this.StartRefreshTimer ();
        }
    }
    
    //--- Style the control buttons.
    GM_addStyle ( "                                                             \
        .GM_ControlWrap {                                                       \
            opacity:            0.8;    /*Coordinate with hover func. */        \
            background:         pink;                                           \
            position:           fixed;                                          \
            padding:            0.6ex;                                          \
            z-index:            666666;                                         \
        }                                                                       \
        .GM_ControlWrap button {                                                \
            padding:            0.2ex 0.5ex;                                    \
            border-radius:      1em;                                            \
            box-shadow:         3px 3px 3px gray;                               \
            cursor:             pointer;                                        \
        }                                                                       \
        .GM_ControlWrap button:hover {                                          \
            color:              red;                                            \
        }                                                                       \
        #GM_StartStopBtn {                                                      \
            top:                0;                                              \
            left:               0;                                              \
        }                                                                       \
        #GM_ClearVisitListBtn {                                                 \
            bottom:             0;                                              \
            right:              0;                                              \
        }                                                                       \
    " );