PHP 매뉴얼에서 함수 복사 단추를 추가하는 사용자 스크립트
10431 단어 PHPuserscriptJavaScriptgreasemonkey
function(, )
클립보드로 복사합니다.PHP 브로셔
라고 반복해서 사용했다.
작업 확인 환경 Windows7
+ Firefox41
+ Greasemonkey
이번에도Firefox
의 스크래치 패드 기능을 이용하여 개발→붙여넣기Greasemonkey
따라서 의존Greasemonkey
은 없을 것이다.
데모
Qita 코드에 복제 버튼을 추가하는 사용자 스크립트
코드
copy_function.user.js
// ==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')
과제.
Qita 코드에 복제 버튼을 추가하는 사용자 스크립트
코드
copy_function.user.js
// ==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')
과제.
// ==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')
설명서의 URL은
http://php.net/manual/*/funciton.*
그럼 다 덮어쓸 수 없을 것 같아서 당분간http://php.net/manual/*/*
하고 있어.함수 페이지를 제외하고는 모두 오류로 인해 멈춘 상태라고 생각합니다.
클립보드를 직접 조작하기가 어려워 대상 문자가 있는 요소를 새로 추가해 복사했다.
Reference
이 문제에 관하여(PHP 매뉴얼에서 함수 복사 단추를 추가하는 사용자 스크립트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/khsk/items/b78315e7024107008956텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)