gpt4 book ai didi

javascript - 使用 CSS 或 Javascript 从 Github Gist 列表中删除/(textnode)

转载 作者:行者123 更新时间:2023-11-28 02:24:02 30 4
gpt4 key购买 nike

我正在尝试从 Github Gists 中的要点列表中删除 username/ 值,问题是没有可用于/值的类/ID 或选择器。

enter image description here

这是 Github Gists 页面和 JSFiddle 的 HTML 和链接:

<div class="d-inline-block">
<span>
<a data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=167012" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self"
href="/hanachin">hanachin</a>
/
<a href="/hanachin/2060751fca3444922386879ac666541e">
<strong class="css-truncate-target">file0.txt</strong>
</a>
</span>
<div class="text-gray" style="font-size:11px;">
Last active
<time-ago datetime="2019-03-08T08:24:35Z">Mar 8, 2019</time-ago>
</div>
<span class="f6 text-gray">
Ruby 2.7 ref:
<a href="https://qiita.com/hanachin_/items/1aa1ba38a87dee91aed6" rel="nofollow">https://qiita.com/hanachin_/items/1aa1ba38a87dee91aed6</a>

</span>
</div>

List of Github Gists

DEMO on JSFiddle

我可以使用下面的代码删除图标和用户名:

[data-hovercard-type="user"] {
display:none;
}

[data-hovercard-type="user"] img {
display:none;
}

但是/没有任何类并且看起来是一个节点/#textnode?我怎样才能访问它?

最佳答案

据我所知,文本节点没有 CSS 选择器。但是要隐藏 /,您可以将其 font-size 设置为 0px

所以一个 CSS 解决方案(虽然不是最好的,因为我们依赖于 dom 结构不改变)

.gist-snippet-meta .d-inline-block > span:first-of-type {
font-size: 0px;
}

.gist-snippet-meta .d-inline-block > span:first-of-type > a:last-of-type {
/* Reset font size back ot what it was before we set it to 0 on the parent */
font-size: 14px;
}

另一种选择是使用 Javascript,

问题是您需要等待 DOM 加载。因此,会有一小段时间您的更改未应用。

此外,您仍然依赖文件名作为父容器中的最后一个 anchor 元素,除非您使用 Regex 获取要点 url(此处牺牲了一些性能)或者您知道用户名

例如,假设用户名保持不变

Array.from(document.querySelector([data-hovercard-type="user"])).forEach( e => {
var parent = e.parentElement;
parent.innerHTML = parent.querySelector('a[href*="hanachin/"]').outerHTML;
});

关于javascript - 使用 CSS 或 Javascript 从 Github Gist 列表中删除/(textnode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55538567/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com