Skip to content

Instantly share code, notes, and snippets.

@oxo-yuta
Last active April 24, 2019 06:31
Show Gist options
  • Save oxo-yuta/919fbff24c7708d476d722ed46237476 to your computer and use it in GitHub Desktop.
Save oxo-yuta/919fbff24c7708d476d722ed46237476 to your computer and use it in GitHub Desktop.
Markdown形式のリンクを作成するChrome拡張機能を作ってみた(メモ) ref: https://qiita.com/spadeloves/items/ff953b8a0c93c0ccf0ab
[表示名](https://hoogehoge.com "title")
{
"manifest_version": 2,
"name": "Markdown Link Maker",
"version": "1.0",
"description": "Copy text the Link text written in Markdown",
"icons": {
"48": "icon/icon48.png"
},
"browser_action": {
"default_icon": {
"48": "icon/icon48.png"
},
"default_title": "Make Markdown Link",
"default_popup": "popup.html"
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="icon.css">
<link rel="stylesheet" href="style.css">
<script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
<div style="width: 200px;">
<p style="margin: 0 auto; display: block;text-align: center;">
Markdown Link Copied!
</p>
</div>
</body>
<script src="js/main.js" type="text/javascript"></script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="icon.css">
<link rel="stylesheet" href="style.css">
<script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
<form id="form">
<div>
<input type="text" name="copy_text">
</div>
</form>
<div>
<button id="copy_button">COPY!!</button>
</div>
</body>
<script src="js/main.js" type="text/javascript"></script>
</html>
console.log("Hello Extension!");
var url = $(location).attr('search');
var title = $("title").text();
console.log("["+ title + "](" + url + '"' + title + '")');
var url = $(location).attr('search');
var title = $("title").text();
console.log("["+ title + "](" + url + '"' + title + '")');
"permissions":["clipboardRead","tabs"]
"permissions":["clipboardRead","tabs"]
//main
chrome.tabs.query({active:true}, function(tab) {
chrome.tabs.sendMessage(tab[0].id, {text:''}, function(response) {
url = tab[0].url;
title = tab[0].title;
result = "["+ title + "](" + url + ' "' + title + '")';
execCopy(result);
});
});
//copy to clipbored
function execCopy(string){
var temp = document.createElement('div');
temp.appendChild(document.createElement('pre')).textContent = string;
var s = temp.style;
s.position = 'fixed';
s.left = '-100%';
document.body.appendChild(temp);
document.getSelection().selectAllChildren(temp);
var result = document.execCommand('copy');
document.body.removeChild(temp);
return result;
}
//main
chrome.tabs.query({active:true}, function(tab) {
chrome.tabs.sendMessage(tab[0].id, {text:''}, function(response) {
url = tab[0].url;
title = tab[0].title;
result = "["+ title + "](" + url + ' "' + title + '")';
execCopy(result);
});
});
//copy to clipbored
function execCopy(string){
var temp = document.createElement('div');
temp.appendChild(document.createElement('pre')).textContent = string;
var s = temp.style;
s.position = 'fixed';
s.left = '-100%';
document.body.appendChild(temp);
document.getSelection().selectAllChildren(temp);
var result = document.execCommand('copy');
document.body.removeChild(temp);
return result;
}
console.log("Hello Extension!");
{
"manifest_version": 2,
"name": "Markdown Link Maker",
"version": "1.0",
"description": "Copy text the Link text written in Markdown",
"icons": {
"48": "icon/icon48.png"
},
"browser_action": {
"default_icon": {
"48": "icon/icon48.png"
},
"default_title": "Make Markdown Link",
"default_popup": "popup.html"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment