PHP 매뉴얼에서 함수 복사 단추를 추가하는 사용자 스크립트

시작
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')

과제.

  • 선택 가능한 매개 변수의 쉼표를 포함할지 여부
  • 여러 함수를 쓴 상황에서 대응
  • 조정 실행 URL
  • 검사를 별로 안 해서 대응할 수 없는 함수 페이지도 많을 것 같습니다.
    설명서의 URL은http://php.net/manual/*/funciton.*그럼 다 덮어쓸 수 없을 것 같아서 당분간http://php.net/manual/*/*하고 있어.
    함수 페이지를 제외하고는 모두 오류로 인해 멈춘 상태라고 생각합니다.
    클립보드를 직접 조작하기가 어려워 대상 문자가 있는 요소를 새로 추가해 복사했다.

    좋은 웹페이지 즐겨찾기