javascript - 用于编辑 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上时)
解决方案
首先,您的代码有许多不需要的冗余。而不是使用整个 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";})();
推荐阅读
- c# - 为什么我不能枚举 X509Store.Certificates
- c# - 在内部函数c#中调用方法
- c++ - 是否可以将数组作为参数传递给函数而不为该数组创建变量?
- string - 如何仅替换 MIPS 中字符串的第一个字符?
- javascript - 三.js定位问题和coneGeometry错误
- c - (C) 我们可以通过哪些方式来操作内存长度和地址?
- c# - InternetSetCookieEx InternetGetCookieEx Cookie 从不/总是过期
- php - Ubuntu LEMP 堆栈中的 Laravel 502 错误网关
- c++ - 如何阻止角色在控制台中移动?
- java - 一旦我运行我的 Appium 脚本,线程“主”java.lang.NoClassDefFoundError 中的异常