![]() ![]() |
Hi, thanks to this forum, I made this script.
So, I would like to know if anyone have any ideas to simplify it.
var hash_html = document.body.innerHTML;
hash_html = hash_html.replace( /\s*([0-9A-F]{40})/ig, ' $1' );
document.body.innerHTML = hash_html;
var hash2_html = document.body.innerHTML;
hash2_html = hash2_html.replace( /\S([0-9A-F]{40})/ig, '$1' );
document.body.innerHTML = hash2_html;
var hash21_html = document.body.innerHTML;
hash21_html = hash21_html.replace( /20([0-9A-F]{40})/ig, '$1' );
document.body.innerHTML = hash21_html;
var hash3_html = document.body.innerHTML;
hash3_html = hash3_html.replace( /\s([0-9A-F]{40})/ig, '< a href=http://www.mysite.net/folder/files/add?info_hash=$1 target=_blank> $1' );
document.body.innerHTML = hash3_html;
var magnet_html = document.body.innerHTML;
magnet_html = magnet_html.replace( /(href="magnet)/ig, 'target=_blank href="http://www.mysite.net/folder/files/add?info_hash=magnet' );
document.body.innerHTML = magnet_html;
Also, hash2_html always ad "20" in front my hash so i have to remove it after with hash21_html, do you know why ? Thanks :) |
![]() ![]() |
Why are you using \S in your pattern? Can you give a before and after example of what you're trying to clean up? Replacing the entire innerHTML of a page likely will be slower than targeting a specific part of the page, but it's hard to get specific without a specific site to examine. Please note that innerHTML also will strip event listeners that are used on some sites. Might not be an issue if you don't care about the rest of the page's functionality. |
![]() ![]() |
Since I use \s* to add a space to the hash, It also add a space to urls with a hash in them, and I don't want that. So I use \S to replace spaces only in urls, so at that point only hashes have a space in front of them, because that's how I target them in my page, since I want it to work every where. And urls with hashes in them are back to normal, exept they have "20" in front of the hash part of the url. So I replace it after and then all urls are back to normal. Here is an example of what it look like at each time :
// "6857823f2cb07881b52857cc17220e60e35d9af8"
// "magnet:?xt=urn:btih:6857823f2cb07881b52857cc17220e60e35d9af8"
var hash_html = document.body.innerHTML;
hash_html = hash_html.replace( /\s*([0-9A-F]{40})/ig, ' $1' );
document.body.innerHTML = hash_html;
// " 6857823f2cb07881b52857cc17220e60e35d9af8"
// "magnet:?xt=urn:btih: 6857823f2cb07881b52857cc17220e60e35d9af8"
var hash2_html = document.body.innerHTML;
hash2_html = hash2_html.replace( /\S([0-9A-F]{40})/ig, '$1' );
document.body.innerHTML = hash2_html;
// " 6857823f2cb07881b52857cc17220e60e35d9af8"
// "magnet:?xt=urn:btih:206857823f2cb07881b52857cc17220e60e35d9af8"
var hash21_html = document.body.innerHTML;
hash21_html = hash21_html.replace( /20([0-9A-F]{40})/ig, '$1' );
document.body.innerHTML = hash21_html;
// " 6857823f2cb07881b52857cc17220e60e35d9af8"
// "magnet:?xt=urn:btih:6857823f2cb07881b52857cc17220e60e35d9af8"
var hash3_html = document.body.innerHTML;
hash3_html = hash3_html.replace( /\s([0-9A-F]{40})/ig, '< a href=http://www.mysite.net/folder/files/add?info_hash=$1 target=_blank> $1' );
document.body.innerHTML = hash3_html;
// " 6857823f2cb07881b52857cc17220e60e35d9af8" link to http://www.mysite.net/folder/files/add?info_hash=6857823f2cb07881b52857cc17220e60e35d9af8
// "magnet:?xt=urn:btih:6857823f2cb07881b52857cc17220e60e35d9af8"
var magnet_html = document.body.innerHTML;
magnet_html = magnet_html.replace( /(href="magnet)/ig, 'target=_blank href="http://www.mysite.net/folder/files/add?info_hash=magnet' );
document.body.innerHTML = magnet_html;
// " 6857823f2cb07881b52857cc17220e60e35d9af8" link to http://www.mysite.net/folder/files/add?info_hash=6857823f2cb07881b52857cc17220e60e35d9af8
// "magnet:?xt=urn:btih:6857823f2cb07881b52857cc17220e60e35d9af8" link to http://www.mysite.net/folder/files/add?info_hash=magnet:?xt=urn:btih:6857823f2cb07881b52857cc17220e60e35
|
![]() ![]() |
How about replacing innerHTML of elements (except 'a' elements) that contain text nodes with hashes:
var textNodes = document.evaluate("//text()", document, null, 7, null);
var i = textNodes.snapshotLength,
re = /[a-f0-9]{40}/ig,
parent, textNode;
while (i--) {
textNode = textNodes.snapshotItem(i);
parent = textNode.parentNode;
if (parent.nodeName != 'A' && re.test(textNode.textContent)) {
parent.innerHTML = parent.innerHTML.replace(re, "<a href='http://site.com/?hash=$&'>$&</a>");
}
}
|
![]() ![]() |
Fido_7up wrote:Use \s+ instead of \s* to avoid that problem.
Edit: Actually that simply collapses 1 or more spaces to 1 space, so that might not accomplish what you want. |
![]() ![]() |
Thank you, it's more than I hoped for :), it works perfectly. Now, my script looks like that :
var magnet_html = document.body.innerHTML;
magnet_html = magnet_html.replace( /(href="magnet)/ig, 'target=_blank href="http://site.com/?hash=magnet' );
document.body.innerHTML = magnet_html;
var textNodes = document.evaluate("//text()", document, null, 7, null);
var i = textNodes.snapshotLength,
re = /[a-f0-9]{40}/ig,
parent, textNode;
while (i--) {
textNode = textNodes.snapshotItem(i);
parent = textNode.parentNode;
if (parent.nodeName != 'A' && re.test(textNode.textContent)) {
parent.innerHTML = parent.innerHTML.replace(re, "< a target=_blank href='http://site.com/?hash=$&'>$&");
}
}
Do you know why Thank you again. |
![]() ![]() |
// magnet links
var magnetLinks = document.querySelectorAll('a[href^="magnet"]');
for (i=0; i<magnetLinks.length; i++) {
magnetLinks[i].target = "_blank";
magnetLinks[i].href = "http://site.com/?hash=" + magnetLinks[i].href;
}
// text nodes with hashes
var textNodes = document.evaluate("//text()", document, null, 7, null);
var i = textNodes.snapshotLength,
re = /[a-f0-9]{40}/ig,
parent, textNode;
while (i--) {
textNode = textNodes.snapshotItem(i);
parent = textNode.parentNode;
if (parent.nodeName != 'A' && re.test(textNode.textContent)) {
parent.innerHTML = parent.innerHTML.replace(re, "< a target=_blank href='http://site.com/?hash=$&'>$&");
}
}
|
![]() ![]() |
Thank you so much, it even solve a problem I had. Thank you for your time :) |

