获取第一个h1标题,并在新选项卡中打开<server_name>h1.html

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

本文关键字:h1 html name server 标题 第一个 新选项 获取 选项      更新时间:2023-09-26

我得到了两个带有以下代码的选项卡-一个表示[object Window],另一个显示我想要的页面。

  1. 怎样才能摆脱第一个无用的标签?

  2. 有没有办法让书签打开http:///getting_started_txt_(random_alphanumeric_code_here).html ?

…我需要打开一个页面,该页面只匹配离线文件名的h1部分,文件名开始,然后是一些胡言乱语。

我这边的脱机文件类似于"getting_started_txt_23468j5jg86458jm34858.html"。因此,bookmarklet必须查找文件名以"带下划线的h1"开头的文件以及其后的任何内容。这可能吗?

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

所以如果我打开了一个页面,第一个标题是h1作为"getting started",书签小工具应该打开一个URL为http://(server_name)/getting_started_txt_(random_alphanumeric_code_here).html的新选项卡。

请注意,服务器上只有一个文件与geting_started_txt部分匹配,文件名的其余部分可以是任何内容。

这样的操作在大多数情况下应该可以工作。

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

bookmarklet应该是这样的:

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

或者这个(url编码的版本):

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

这些是JavaScript代码的简化版本:

// 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);