Last active
July 24, 2019 05:33
-
-
Save skysan87/b3b6f8d5753e51a61b990f3f0a6a521e to your computer and use it in GitHub Desktop.
[Chrome拡張] 表示しているページのタイトルとURLをmarkdown形式でクリップボードへコピー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "markdown_url", | |
"version": "1.0.0", | |
"description": "format the page link as markdown", | |
"background": { | |
"scripts": ["script.js"], | |
"persistent": false | |
}, | |
"permissions": [ | |
"clipboardRead", | |
"contextMenus", | |
"tabs" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 参考 | |
// https://qiita.com/MugeSo/items/e5307bda346c0bb8e22e | |
// http://zentoo.hatenablog.com/entry/2014/01/26/200914 | |
const ContextMenus = new function () { | |
let items = {}; | |
let callbacks = {}; | |
this.setItems = function (aItems) { | |
aItems.forEach(function (item) { | |
callbacks[item.id] = item.onclick; | |
item.onclick = null; | |
items[item.id] = item; | |
}); | |
}; | |
this.create = function () { | |
Object.keys(items).forEach( | |
function (key) { | |
chrome.contextMenus.create(items[key]); | |
} | |
); | |
}; | |
chrome.contextMenus.onClicked.addListener(function (info, tab) { | |
callbacks[info.menuItemId](info, tab); | |
}); | |
}; | |
ContextMenus.setItems([ | |
{ | |
type: 'normal', | |
id: 'copy_pageUrl', | |
title: "[md]このページのurlをコピー", | |
onclick: copyPageUrl | |
} | |
]); | |
chrome.runtime.onInstalled.addListener(ContextMenus.create); | |
function copyPageUrl(info, tab) { | |
let title = tab.title | |
let url = tab.url | |
if(title !== '' && url !== '') | |
saveToClipboad(`[${title}](${url})`) | |
} | |
function saveToClipboad(str) { | |
var textArea = document.createElement("textarea"); | |
textArea.style.cssText = "position:absolute;left:-100%"; | |
document.body.appendChild(textArea); | |
textArea.value = str; | |
textArea.select(); | |
document.execCommand("copy"); | |
document.body.removeChild(textArea); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment