2

誰かが私を助けてくれることを願っています。js関数を拡張子で再宣言したい。たとえば、Web サイトには基本的な js 関数があります。

function foo(){
..something here..
}

同名の独自関数で再宣言したい。どのようにすれば最も簡単になるでしょうか?

編集 1.私はよりよく説明しようとします。

ウェブサイトにネイティブコードがあります:

Notifier = {
  debug: false,
  init: function (options) {
    curNotifier = extend({
      q_events: [],
      q_shown: [],
      q_closed: [],
      q_max: 3,
      q_idle_max: 5,
      done_events: {},
      addQueues: curNotifier.addQueues || {},
      recvClbks: curNotifier.recvClbks || {},
      error_timeout: 1,
      sound: new Sound('mp3/bb1'),
      sound_im: new Sound('mp3/bb2')
    }, options);

    if (!this.initFrameTransport() && !this.initFlashTransport(options)) {
      return false;
    }
    this.initIdleMan();

    if (!(curNotifier.cont = ge('notifiers_wrap'))) {
      bodyNode.insertBefore(curNotifier.cont = ce('div', {id: 'notifiers_wrap', className: 'fixed'}), ge('page_wrap'));
    }
  },
  destroy: function () {
    Notifier.hideAllEvents();
    curNotifier.idle_manager.stop();
    curNotifier = {};
    re('notifiers_wrap');
    re('queue_transport_wrap');
  },
  reinit: function () {
    ajax.post('notifier.php?act=a_get_params', {}, {
      onDone: function (options) {
        if (options) {
          curNotifier.error_timeout = 1;
          this.init(options);
        } else {
          curNotifier.error_timeout = curNotifier.error_timeout || 1;
          setTimeout(this.reinit.bind(this), curNotifier.error_timeout * 1000);
          if (curNotifier.error_timeout < 256) {
            curNotifier.error_timeout *= 2;
          }
        }
      }.bind(this),
      onFail: function () {
        curNotifier.error_timeout = curNotifier.error_timeout || 1;
        setTimeout(this.reinit.bind(this), curNotifier.error_timeout * 1000);
        if (curNotifier.error_timeout < 256) {
          curNotifier.error_timeout *= 2;
        }
        return true;
      }.bind(this)
    });
  }
}

と機能サウンド

function Sound(filename) {
  var audioObjSupport = false, audioTagSupport = false, self = this, ext;
  if (!filename) throw 'Undefined filename';

  try {
    var audioObj = ce('audio');
    audioObjSupport = !!(audioObj.canPlayType);

    if (('no' != audioObj.canPlayType('audio/mpeg')) && ('' != audioObj.canPlayType('audio/mpeg')))
      ext = '.mp3?1';
    else if (('no' != audioObj.canPlayType('audio/ogg; codecs="vorbis"')) && ('' != audioObj.canPlayType('audio/ogg; codecs="vorbis"')))
      ext = '.ogg?1';
    else
      audioObjSupport = false;
  } catch (e) {}
  // audioObjSupport = false;

  if (audioObjSupport) {
    audioObj.src = filename + ext;
    var ended = false;
    audioObj.addEventListener('ended', function(){ended = true;}, true);
    audioObj.load();
    this.playSound = function() {
      if (ended) {
        audioObj.load();
      }
      audioObj.play();
      ended = false;
    };
    this.pauseSound = function() {
      audioObj.pause();
    };
  } else {
    cur.__sound_guid = cur.__sound_guid || 0;
    var wrap = ge('flash_sounds_wrap') || utilsNode.appendChild(ce('span', {id: 'flash_sounds_wrap'})),
        guid = 'flash_sound_' + (cur.__sound_guid++);

    var opts = {
      url: '/swf/audio_lite.swf?4',
      id: guid
    }
    var params = {
      swliveconnect: 'true',
      allowscriptaccess: 'always',
      wmode: 'opaque'
    }
    if (renderFlash(wrap, opts, params, {})) {
      var swfObj = browser.msie ? window[guid] : document[guid],
          inited = false,
          checkLoadInt = setInterval(function () {
        if (swfObj && swfObj.paused) {
          try {
            swfObj.setVolume(1);
            swfObj.loadAudio(filename + ext);
            swfObj.pauseAudio();
          } catch (e) {debugLog(e);}
        }
        inited = true;
        clearInterval(checkLoadInt);
      }, 300);
      self.playSound = function() {
        if (!inited) return;
        swfObj.playAudio(0);
      };
      self.pauseSound = function() {
        if (!inited) return;
        swfObj.pauseAudio();
      };
    }
  }
}
Sound.prototype = {
  play: function() {
    try {this.playSound();} catch(e){}
  },
  pause: function() {
    try {this.pauseSound();} catch(e){}
  }
};

再宣言機能Soundでインジェクションを追加しようとするとうまくいきません。たとえば、xSound などの独自の関数を作成し、次のようにします。

cur.sound = new xSound('mp3/bb1');

それは働いています。

4

3 に答える 3

8

たとえば、次のようにできます。

foo = function(args) {
    // method body...
}

JavaScript は、関数が第一級市民であるため、他の型のように操作できるプログラミング言語です。

アップデート:

このコードが最初の定義ではなく、実際に再定義を行うことを確認してください。(@jmort253 に感謝)

于 2012-11-01T01:27:18.247 に答える
1
function foo(){
   // ..something else here..
}
于 2012-11-01T01:26:34.047 に答える
0

拡張機能のコンテンツ スクリプト コードとウェブページ コードは、異なる実行コンテキストで実行されることに注意してください。

したがって、Web ページのコンテキストに存在する関数を再定義する場合は、コードを Web ページに挿入する必要があります。それを行うためのさまざまな方法については、Rob W によるこの回答をご覧ください: コンテンツ スクリプトを使用してページ コンテキストにコードを挿入する

于 2012-11-01T07:51:55.680 に答える