如何检测页面上已访问和未访问的链接?
我的目标是检测网页上未访问的链接,然后创建一个greasemonkey 脚本来单击这些链接。这里的未访问链接是指我没有打开的链接。由于我可以看到所有浏览器都提供了更改已访问和未访问链接颜色的功能,因此可以以任何方式检测这些链接。 在搜索时我发现了这个链接: http://www.mozdev.org /pipermail/greasemonkey/2005-November/006821.html 但这里有人告诉我这不再可能了。请帮忙。
My aim is to detect the unvisited links on a webpage and then create a greasemonkey script to click on those links. By unvisited links here I mean the links which are not opened by me. Since I can see all the browser provide capability to change the color of visited and unvisited link is it possible to detect these links in any manner.
While searching I came upon this link: http://www.mozdev.org/pipermail/greasemonkey/2005-November/006821.html but someone here told me that this is no longer possible. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的,javascript 不可能检测在 Firefox 或 Chrome 中是否访问了链接——这是唯一适用于此 Greasemonkey 上下文的浏览器。
这是因为 Firefox 和 Chrome 非常重视安全和隐私。来自 CSS2 规范:
另请参阅“隐私和 :visited 选择器”
您可以看到一个演示,显示安全浏览器不会让您嗅探访问过的链接 jsfiddle.net/n8F9U。
根据您的具体情况,由于您正在访问页面并保持其打开状态,因此您可以帮助脚本跟踪访问了哪些链接。这并不是万无一失的,但我相信它会满足您的要求。
首先,通过执行以下操作来查看正在运行的脚本:
测试页面每次重新加载或刷新时都会添加一个新链接。
GM 脚本向其运行的页面添加了 2 个按钮。左上角有一个“开始/停止”按钮,右下角有一个“清除”按钮。
当您按下“开始”按钮时,它会执行以下操作:
“清除”按钮可删除访问过的页面列表。
警告:如果您在刷新循环处于活动状态时按“清除”,则下次重新加载页面时,所有链接都将在新选项卡中打开。
接下来,在您的网站上使用该脚本...
仔细阅读脚本中的注释,您必须更改
@include
、@exclude 和 selectorStr
值以匹配您正在使用的网站。为了获得最佳结果,禁用任何“重新加载每个”附加组件或“自动更新”选项。
重要说明:
脚本必须使用永久存储来跟踪链接。< br>
选项包括:cookie、
sessionStorage
、localStorage
,globalStorage
,GM_setValue()
和IndexedDB
。这些都有缺点,在这种情况下(单个站点、可能存在大量链接、多个会话),
localStorage
是最佳选择(IndexedDB
可能是,但它仍然太不稳定——导致我的机器上经常出现 FF 崩溃)。这意味着只能在每个站点的基础上跟踪链接,并且“安全”、“隐私”或“清洁”实用程序可以阻止或删除已访问链接的列表。 (就像清除浏览器的历史记录将重置已访问链接的所有 CSS 样式一样。)
该脚本目前仅适用于 Firefox。即使安装了 Tampermonkey,如果不进行一些重新设计,它也不应该在 Chrome 上运行。
脚本:
Correct, it is not possible for javascript to detect if a link is visited in either Firefox or Chrome -- which are the only 2 browsers applicable in this Greasemonkey context.
That is because Firefox and Chrome take security and privacy seriously. From the CSS2 spec:
See also, "Privacy and the :visited selector"
You can see a demo showing that secure-ish browsers will not let you sniff visited links at jsfiddle.net/n8F9U.
For your specific situation, because you are visiting a page and keeping it open, you can help a script keep track of what links were visited. It's not fool-proof, but I believe it will do what you've asked for.
First, see the script in action by doing the following:
The test page adds a new link, every time it is reloaded or refreshed.
The GM script adds 2 buttons to the pages it runs on. A "start/Stop" button in the upper left and a "Clear" button in the lower right.
When you press the "Start" button, it does the following:
The "Clear" button, erases the list of visited pages.
WARNING: If you press "Clear" while the refresh loop is active, then the next time the page reloads, all links will be opened in new tabs.
Next, to use the script on your site...
Carefully read the comments in the script, you will have to change the
@include
,@exclude
, andselectorStr
values to match the site you are using.For best results, disable any "Reload Every" add-ons, or "Autoupdate" options.
Important notes:
The script has to use permanent storage to to track the links.
The options are: cookies,
sessionStorage
,localStorage
,globalStorage
,GM_setValue()
, andIndexedDB
.These all have drawbacks, and in this case (single site, potentially huge number of links, multiple sessions),
localStorage
is the best choice (IndexedDB
might be, but it is still too unstable -- causing frequent FF crashes on my machine).This means that links can only be tracked on a per-site basis, and that "security", "privacy", or "cleaner" utilities can block or erase the list of visited links. (Just like, clearing the browser's history will reset any CSS styling for visited links.)
The script is Firefox-only, for now. It should not work on Chrome, even with Tampermonkey installed, without a little re-engineering.
The script:
您可以解析页面上的所有链接并获取它们的 CSS 颜色属性。如果链接的颜色与您在 CSS 中定义的未访问链接的颜色匹配,则该链接未被访问。
这种技术通常用于确定所有访问过的链接。
这是一种安全漏洞,可让您确定用户是否访问了特定网站。通常被卑鄙的营销人员使用。
这种技巧通常被归类为“浏览器历史记录操纵技巧”。
更多信息与代码:http://www.stevenyork.com/tutorial/getting_browser_history_using_javascript
You can parse all links on the page and and get their CSS color property. If a color of the link is a match to the color of unvisited link you defined in CSS the this link is unvisited.
This kind of technique usually used to determine all visited links.
This is sort of a security breach that allows you to determine if user visited particular web-site. Usually used by sleazy marketers.
This kind of tricks usually classifies as a "browser's history manipulation tricks".
More info with code: http://www.stevenyork.com/tutorial/getting_browser_history_using_javascript