Copy clean markdown link
Installation
share
←Please drag & drop this button to bookmark bar
or copy & paste code below
Description
ページのリンクをmarkdown形式でコピーします。 ただし、人に共有することを考えて、URLのクリーン処理を行います。
- github以外のページで、ハッシュの削除を行います。
- Amazon, Monotaroの商品ページのURLを短縮します。
現在のページのリンクをmarkdown形式でコピーします。
Source code
copy_md_link_full.js// preprocess
let url = location.href;
url = shortenAmazon(url);
url = shortenMonotaro(url);
url = removeHash(url);
history.replaceState("", document.title, url);
// main
if (window.confirm("Markdown?")) {
copyMarkdownToClipboard()
window.alert("Markdown link copied")
} else {
copyURLWithTitleToClipboard()
window.alert("Title and URL copied")
}
// end of main
function shortenAmazon(url) {
const m = url.match(/https:\/\/www\.amazon\.co\.jp\/.*(dp|product)\/([0-9A-Z]{10})\/?/);
if (!m) return url;
const p = m[2];
return `https://www.amazon.co.jp/dp/${p}/`;
}
function shortenMonotaro(url) {
const m = url.match(/https:\/\/www\.monotaro\.com\/g\/([0-9]+)\//);
if (!m) return url;
return m[0];
}
function removeHash(url) {
const excludeDomain = [
"github.com",
];
console.log({url})
const urlObj = new URL(url);
console.log({hostname: urlObj.hostname})
console.log({excludeDomain})
console.log(excludeDomain.includes(urlObj.hostname));
if (excludeDomain.includes(urlObj.hostname)) {
return url;
}
urlObj.hash = '';
return urlObj.toString();
}
function copyURLWithTitleToClipboard() {
const url = location.href;
const title = document.title;
navigator.clipboard.writeText(`${title} ${url}`);
}
function copyMarkdownToClipboard() {
const url = location.href;
const title = document.title;
navigator.clipboard.writeText(`[${title}](${url})`);
}
Minified source code
javascript:!function(){let o=location.href;o=function(o){const t=o.match(/https:\/\/www\.amazon\.co\.jp\/.*(dp|product)\/([0-9A-Z]{10})\/?/);if(!t)return o;return`https://www.amazon.co.jp/dp/${t[2]}/`}(o),o=function(o){const t=o.match(/https:\/\/www\.monotaro\.com\/g\/([0-9]+)\//);return t?t[0]:o}(o),o=function(o){const t=["github.com"];console.log({url:o});const n=new URL(o);if(console.log({hostname:n.hostname}),console.log({excludeDomain:t}),console.log(t.includes(n.hostname)),t.includes(n.hostname))return o;return n.hash="",n.toString()}(o),history.replaceState("",document.title,o),window.confirm("Markdown?")?(!function(){const o=location.href,t=document.title;navigator.clipboard.writeText(`[${t}](${o})`)}(),window.alert("Markdown link copied")):(!function(){const o=location.href,t=document.title;navigator.clipboard.writeText(`${t} ${o}`)}(),window.alert("Title and URL copied"))}();
Edit this page