Is this possible?
|
|
Well I was going to attempt to make or request a script for urlcash.net and similar sites in which links would automatically get converted from www.spam.urlcash.net to www.originallink.com. Now urlcash stores the actual link in the <title> part of the webpage source and other similar sites usually have it already on that page and is not automatically generated. Now my question is, can greasemonkey get the source code of a webpage link without it being clicked? I imagine the script would look something like this if possible:
|
|
|
Yep, it can be done with an XMLHttpRequest(), (assuming the source is on the same domain, you'll need to resort to a GM_XMLHttpRequest if it's not) However it's unfortunately not possible to get just the first 5kb. You'll have to download the entire page source and then parse through it. So something like:
if(window.ActiveXObject){ //for IE
var httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){ //for real browsers
var httpRequest=new XMLHttpRequest();
}
httpRequest.open("GET",'http://spam.urlcash.net/',true); //open the request
httpRequest.onreadystatechange=function(){ //set a function that runs on every readystatechange (i.e. listens for the Req to complete
if(httpRequest.readyState==4){ // Request is complete
if(httpRequest.status==200){ // Request completed successfully
var requestParts=httpRequest.responseText.split('<title>');
var originallink=requestParts[1].split('</title>');
location.href=originallink;
}
else{ // Request completed unsuccessfully
window.console=window.console||{log:GM_log}||{log:opera.postError}||{log:alert};
console.log("UserScript:Error loading urlcash link\n"+httpRequest.status+":"+httpRequest.statusText);
}
}
};
httpRequest.send(null); // set the Request in motion
Haven't tested that so I'd recommend reading http://developer.mozilla.org/en/docs/XMLHttpReq... if you've any problems. |
|
|
Disregard the above post since it won't work for cross-domain requests. Read this instead, run the example to see how it works, and proceed with your script. |
|
|
@Mikado
I wrote that on the assumption that the links ARE on the same domain as they're being clicked from, with the desired target (originallink) being the one on a different domain. Sorry if I'm incorrect in this assumption, but I thought I was fairly clear. |
|
|
Yes you were, but you've totally missed the point. It's crystal clear that links are pointing to another domain, as well as that location shouldn't be changed right away - guy wants to overwrite links' hrefs. Adding that |
|
|
Thanks Mikado for pointing out the window.console error. I hadn't tested but good to know for future reference. Sorry if the separate domains didn't seem quite as 'crystal clear' to my apparently slow mind. My thinking was that links were clicked on the I wasn't all that familiar with the service urlcash was offering, and I now realise the links are on any number of domains. My bad. @magixx
GM_xmlhttpRequest({method:'GET',url:'http://spam.urlcash.net',onload:function(responseDetails){
var requestParts=responseDetails.responseText.split('<title>');
var originallink=requestParts[1].split('</title>')[0];
location.href=originallink;
}
});
As Mikado pointed out, the last line goes to the page immediately. Obviously this wouldn't be what you really want to happen, but you would need to iterate through the links with this function somehow (and run a lot of XHRs), so that's extra code. This is simply a demonstration of the method. Again this is not tested, so hopefully Mikado can help me out and set any of my silly oversights straight if needed. Edit: And thank you Mikado for doing just that...grrr, typos. This is why my scripts always need debugging. |
|
|
Hmm... It seems to semi-work, I disabled noscript (which was already set to allow everything), stopped all other greasemonkey scripts, and made it website specific I got this:
Edit:
|
|
|
var originallink=requestParts[1].split('</title>')[0];
|
|
|
This is what I came up with: // ==UserScript==
// @name UrlCash Filter
// @description None
// @include *
// ==/UserScript==
var re = new RegExp('(http://[a-z0-9\-]*\.(urlcash\.net|usercash.com))', "i");
var rt = new RegExp('<title>([^<]*)</title>', "i");
function clickHandler(event) {
var url = event.currentTarget.getAttribute('href');
GM_xmlhttpRequest({method: 'GET', url: url, onload: function(response){
var t = response.responseText;
var m = rt.exec(t);
if (m != null) GM_openInTab(m[1]);
}});
event.preventDefault();
}
var links = document.getElementsByTagName('a');
for (var i=0; i<links.length; i++) {
var href = links[i].getAttribute('href');
var m = re.exec(href);
if (m != null) links[i].addEventListener('click', clickHandler, false);
}
Initially I wrote a script which rewrote all of the links on the page, but as you can imagine that generated a large number of requests and spammed the local net connection to death. So I swapped strategy and hooked the click event on each of the links instead. When a link is clicked it does the page fetch, grabs the wrapped URL, and opens that URL in a new tab. |
|
|
Thank you very much, hopefully I can also adjust this script for other annoying redirect sites. |
|
|
Well, this script does not work for me. Nothing happens. I really like to get rid of the manual clicking when am visiting sites behind usercash.com. Am on firefox 3.x and ubuntu linux 8.04. |
|
|
Which is exactly what it does do - for me. A page full of urlcash / usercash links gets modified and a javascript handler attached. Clicking the left mouse button opens the linked content in another tab. Note it won't work with middle-click (only catches left click), and might take a while to work (i.e. has page loaded completely). A link to the page that isn't working would be helpful. I'll test it out on ubuntu, and see if it works there for me :) |
|
|
Aaa, I was clicking with the middle mouse button to always open it in another tab. If I just click on the usercash url, it will open in a background tab and some way (your nice script) redirects me to the target url directly. Many thanks Wobble. You have saved me from a alot of anger about this usercash shit :-) |
|
|
Another way to get rid of the manual click caused by usercash is to write a script that grabs the page title and redirects the browser to that url and set the script to run on *.usercash.com/* You wont get the links on the page fixed as you do using GM_xmlhttpRequest but on the other hand this might save you some requests too (if you don't open all links on the page). Might be nice if the page contains lots of "spam" links. And the script doesn't have to run on every page you visit. Here's how I "fast forward" on linkbuck links (another linkspammer service).
// ==UserScript==
// @name Linkbuck Filter
// @description "Fast forward" on linkbucks ad pages
// @include *.linkbucks.com/*
// ==/UserScript==
( function () {
var redirectToUrl;
if (redirectToUrl = document.getElementById("linkBucksSkip").href)
location.replace(redirectToUrl);
} () );
|
|
|
Ah very neat! That even solves the "middle click" problem (not caught by the onclick event handler). So a usercash script simply becomes something like this: // ==UserScript==
// @name URLCash / UserCash filter
// @description Removes wrapper frame on image pages
// @include *.usercash.com/*
// ==/UserScript==
var re = new RegExp('(http://.*)');
m = re.exec(document.title);
if (m != null) location.replace(m[1]);
|
