javascript - 重新声明 JavaScript 函数

标签 javascript jquery google-chrome-extension

我希望有人能帮助我。 我想通过扩展重新声明js函数。 例如网站上有基本的js功能:

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

我想用自己的同名函数重新声明它。怎么做最容易?

编辑1。我会尽力解释得更好。

网站中有一个 native 代码:

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){}
  }
};

当我尝试使用重新声明函数声音添加注入(inject)时,它不起作用。 如果我创建自己的函数,例如 xSound 并以这种方式进行操作:

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

它正在工作。

最佳答案

你可以这样做,例如:

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

JavaScript 是一种编程语言,其中函数是一等公民,因此您可以像其他类型一样操作它们。

更新:

确保这段代码实际上执行的是重新定义,而不是第一个定义。 (感谢@jmort253)

关于javascript - 重新声明 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13169697/

相关文章:

javascript - 如何在我的 chrome 扩展中本地保存信息?

javascript - 微信分享,如何更改转发描述和缩略图?

javascript - 从 3rd 方网站 API 获取值

javascript - 使用链接点击清除输入字段 Javascript

javascript - 如何在Shiny中用JavaScript改变UI元素的属性?

javascript - 如何确保加载的文件可以被js代码检测到

javascript - 使用 jquery 和 javascript 遍历 dom 的正确方法以及 "this"的用法

windows-7 - 如何可靠地给予窗口焦点?

jquery - 在这种情况下如何删除所有 CSS 属性

javascript - 为什么后台js处理时扩展程序的弹出窗口会被阻止?