首页 > 解决方案 > 用于编辑 URL 和添加文本的书签

问题描述

我想使用书签来编辑我的实时 URL,以便在预览和编辑模式之间切换。

我已经能够使用小书签来编辑大部分网址。但是,我还需要它在 url 的末尾添加文本以使其正常工作。下面的例子

当前网址:

https://www.site.co.uk/part1/part2

预览网址 https://author-cms.net/content/site-co-uk/en/part1/part2.html?wcmmode=disabled

这是“html?wcmmode=disabled”,我似乎不知道如何添加。

我当前的代码是:javascript:(function(){var url=window.location.href;stringUrl=String(url);stringUrl=stringUrl.replace(/www.site.co.uk/, "author-cms.net /content/site-co-uk/en");document.location=stringUrl;})()

这当前将链接加载为(在https://www.site.co.uk/part1/part2上时)

https://author-cms.net/content/site-co-uk/en/part1/part2

标签: javascriptbookmarklet

解决方案


首先,您的代码有许多不需要的冗余。而不是使用整个 url,window.location.href您可以使用它window.location.pathname来获取路径(第一个斜杠之后的所有内容,在您的示例中为/part1/part2)。然后,您可以https://author-cms.net/content/site-co-uk/en像这样添加到路径:"https://author-cms.net/content/site-co-uk/en" + window.location.pathname

要将文本添加到 url 的末尾,如果保证 url 以文本结尾part2并且永远不会以文件扩展名结尾,part2.html那么您只需将字符串连接到 url 的末尾即可url + ".html?wcmmode=disabled"

这是一段非常简单的代码,可以完成工作,但不能适应很多情况。

javascript:(function(){window.location.href="https://author-cms.net/content/site-co-uk/en"+window.location.pathname+".html?wcmmode=disabled";})();

推荐阅读