javascript - Modify the file before it opens and then open it in a new tab
问题描述
The site has a <a/>
link with the href
attribute.
If you click on it, the file specified in the href
will open in a new tab.
I need javascript or jQuery that will modify the file before it opens and then open it in a new tab.
Ok. more info. Sorry for my bad English.
There is an element on the page:
<a href="https://example.com/something.user.css" target="_blank">Install usercss</a>
If you click on it the browser will open a new tab with this file and the Stylus extension offer to install this style. But this style is broken.
I want to write userscript that will download this file, delete broken part in it and open it in the new tab so that the Stylus pick it up and offer to install the fixed style.
解决方案
我写的代码很笼统,记住这一点。我写它是因为你没有具体说明你想要什么。您可能不使用 javascript 指定它来打开单独的页面。目标参数将起作用。
html:
<a href="#" id="link">click it</a> // your link in the page
Javascript:
const link = document.getElementById("link");
changeHref(link, "go somewhere", true);
function changeHref(link, href, blank) {
link.addEventListener("click", () => {
link.setAttribute("href", "go somewhere");
blank && link.setAttribute("target", "_blank"); // if attribute true thats block works
})
};
当你以这种方式创建一个函数时,你可以给它任何你想要的参数并为你想要的任何链接执行它。
祝你好运!
推荐阅读
- express - TypeORM 批量插入?
- c++ - 用于排序算法的 C++ lambda
- docker - 减少 docker build 的输出
- c++ - QThread 没有停止
- git - Github api 完成对给定存储库分支的所有提交
- javascript - 无效的表单正文 embed.image.url:无法将“{}”解释为字符串
- intellij-idea - 如何隐藏 Find Usages 操作后可见的预览区域?
- node.js - Ytdl-core 无法正常工作 - “找不到播放器配置”
- mysql - 是否可以在单个查询中使用来自不同表中多个列的多个值来输出计算?
- reinforcement-learning - 使用大众封面的意外预测行为