开发者

Get first h1 heading and open \\<server_name>\h1.html in new tab

开发者 https://www.devze.com 2023-03-22 12:38 出处:网络
I get two tabs with the following code - one that says [object Window] and other displays the page I want.

I get two tabs with the following code - one that says [object Window] and other displays the page I want.

  1. What wi开发者_开发问答ll get rid of the first useless tab?

  2. Is there a way to get the bookmarklet open http:///getting_started_txt_(random_alphanumeric_code_here).html ?.

...I need to open a page that matches just the h1 part of the offline file name with which the file name begins and then some gibberish.

The offline files at my end are like "getting_started_txt_23468j5jg86458jm34858.html". So the bookmarklet must look for the file where the filename begins with "h1 with underscores" and anything after it. Is this possible?

window.open('http://en.wikipedia.org/wiki/' + document.getElementsByTagName('h1')[0].innerHTML.replace(/<[^>]+>/g, '').replace(/ /g, '_') + '_txt_');

So If I have a page open with first heading h1 as "getting started", the bookmarklet should open a new tab with the URL http://(server_name)/getting_started_txt_(random_alphanumeric_code_here).html.

Note that there is only one file on the server which matches that getting_started_txt part and the rest of the file name can be anything.


Something like this should work in most cases.

window.open('\\\\server_name\\en\\' + encodeURIComponent(document.getElementsByTagName('h1')[0].innerHTML.replace(/<[^>]*>/g, '').replace(/\s/g, '_')) + '.html', 'win')


The bookmarklet would look like this:

javascript:window.open('http://en.wikipedia.org/wiki/' + document.getElementsByTagName('h1')[0].innerHTML.replace(/<[^>]+>/g, '').replace(/ /g, '_'));

Or this (URL-encoded version):

javascript:window.open%28%27http%3A//en.wikipedia.org/wiki/%27%20%252B%20document.getElementsByTagName%28%27h1%27%29%5B0%5D.innerHTML.replace%28/%3C%5B%5E%3E%5D%252B%3E/g%2C%20%27%27%29.replace%28/%20/g%2C%20%27_%27%29%29%3B

Which are the reduced version of this JavaScript code:

// Find the first H1 node
var h1 = document.getElementsByTagName('h1')[0];
// Extract the content of the node
var title = h1.innerHTML;
// Delete HTML tags in the content of the title
title = title.replace(/<[^>]+>/g, '');
// Replace spaces with underscore symbols
title = title.replace(/%s/g, '_');
// Open a new window (or tab) with the corresponding Wikipedia article
window.open('http://en.wikipedia.org/wiki/' + title);
0

精彩评论

暂无评论...
验证码 换一张
取 消