首页 > 解决方案 > 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.

标签: javascriptjquery

解决方案


我写的代码很笼统,记住这一点。我写它是因为你没有具体说明你想要什么。您可能不使用 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
    })
};

当你以这种方式创建一个函数时,你可以给它任何你想要的参数并为你想要的任何链接执行它。

祝你好运!


推荐阅读