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.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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
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: