Last active
October 20, 2015 00:58
-
-
Save khsk/8a67fb0fdb968d5a6114 to your computer and use it in GitHub Desktop.
PHPマニュアルから関数をコピーするボタンを追加するユーザースクリプト ref: http://qiita.com/khsk/items/b78315e7024107008956
This file contains 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
// ==UserScript== | |
// @name copy function | |
// @namespace khsk | |
// @description 関数説明から関数をコピーするスクリプト | |
// @include http://php.net/manual/*/* | |
// @include http://www.php.net/manual/*/* | |
// @include http://jp.php.net/manual/*/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
console.time('Copy function') | |
delete console.log; | |
var button = document.createElement('button') | |
button.innerHTML = 'Copy' | |
button.addEventListener('click', function(e){ | |
e.preventDefault() | |
if (!document.queryCommandSupported('copy')) { | |
alert('copyに対応していません') | |
return | |
} | |
if (typeof span === "undefined") { | |
// グローバルにコピーする要素を作成する | |
span = document.createElement('span') | |
// 関数名の取得 | |
var methodName = document.getElementsByClassName('methodname')[0].textContent | |
// ,数の取得 | |
var comma = ''; | |
var method = document.getElementsByClassName('methodsynopsis dc-description')[0].textContent | |
var pos = method.indexOf(",") | |
while ( pos != -1 ) { | |
comma += ', ' | |
pos = method.indexOf(",", pos + 1); | |
} | |
span.innerHTML = methodName + '(' + comma + ')' | |
span.id = 'userCopy' | |
document.getElementsByTagName('body')[0].appendChild(span) | |
} | |
// 手動の選択状態を解除する | |
window.getSelection().removeAllRanges(); | |
// コピー対象を選択状態にする | |
// 表示されないと選択できないので一時解除 | |
span.style.display = ''; | |
var range = document.createRange() | |
range.selectNode(span) | |
window.getSelection().addRange(range) | |
try { | |
document.execCommand('copy') | |
} catch (e) { | |
alert('copyに失敗しました') | |
} | |
// 選択を解除する | |
window.getSelection().removeAllRanges(); | |
span.style.display = 'none'; | |
}) | |
document.getElementsByClassName('methodsynopsis dc-description')[0].appendChild(button) | |
console.timeEnd('Copy function') | |
This file contains 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
function(, ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment