微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

书签:转到页面并填写表格

如何解决书签:转到页面并填写表格

我想为 a site where you can watch videos with any subtitle .srt file 制作一个书签。目的是我可以直接从 YouTube(或 DailyMotion 或 ...)视频页面点击它,并立即被传送到 https://tubecc.herokuapp.com/,所需的视频网址 已经填写 .

我目前的尝试是:

javascript:{vidurl=document.location.href;document.location.href=('https://tubecc.herokuapp.com');document.getElementById("url").value=vidurl;void(0);}

但是,所有这些都是导航到网站 - 它不会填写框中的网址。

当我尝试用一​​些无意义的文本作为单独的操作填充 url 框时 - 即我已经在 https://tubecc.herokuapp.com/ 上并且只是从控制台运行 document.getElementById("url").value="foobar",或者制作一个单独的书签只包含 javascript:{document.getElementById("url").value="foobar";void(0);},它确实工作 - 'foobar' 出现在文本框中,正如预期的那样。

它似乎只有在与首先导航到页面的较大书签结合使用时才会失败。一度我怀疑这与异步性有关,因此我为填充 (javascript:{vidurl=document.location.href;document.location.href('https://tubecc.herokuapp.com');setTimeout(() => { document.getElementById("url").value="foobar"; },2000);void(0);}) 添加了延迟,但这似乎也无济于事。

PS:不是 this question 的副本 - 那里描述了基本原理,我正在应用它。我的问题是为什么在这种特定情况下会出错。

解决方法

当您设置 location.href 时,浏览器导航到新页面并卸载所有当前运行的 JavaScript。

要实现您想要的效果,您还需要一个浏览器扩展程序 User JavaScript and CSS(或任何像 Tampermonkey 这样的用户脚本管理器)来在 tubecc.herokuapp.com 上自动运行 JavaScript 代码。

使用此书签导航到 tubecc.herokuapp.com

javascript: (() => {
  document.location.href = `https://tubecc.herokuapp.com?url=${document.location.href}`;
})();

在浏览器扩展中使用此代码:

const params = new URLSearchParams(document.location.search);
const url = params.get('url');

url && (document.getElementById('url').value = url);
,

再看这一行:

javascript:{vidurl=document.location.href;document.location.href=('https://tubecc.herokuapp.com');document.getElementById("url").value=vidurl;void(0);}

当我们在网页上运行此程序时,document.location.href=('https://tubecc.herokuapp.com') 会更改位置。因此,当加载“https://tubecc.herokuapp.com”时,没有运行 javascript 代码。

为了实现您正在寻找的功能,您可以使用 localstorage 将视频 URL 存储在浏览器中。然后重定向到您的页面。

最后,您需要在“https://tubecc.herokuapp.com”网页中添加启动脚本。

书签:

window.localStorage.setItem('videoURL')=document.location.href;
document.location.href('https://tubecc.herokuapp.com')

在“https://tubecc.herokuapp.com”中启动脚本:

let url = window.localStorage.getItem('videoURL');
document.getElementById("url").value=url;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。