0

私の Firefox 拡張機能は、次のことを行う必要があります。

  1. 現在のページのアドレスを保存する
  2. 新しいページを開く
  3. ボタンが押された場合、アドレスを新しいページのコンテンツに挿入します

問題は、このアドレスを変数に保存する方法です。最初のページの関数を介してアドレスが変数に保存されている場合、ボタンが押されたときにアドレスを表示する必要がある新しいページの関数には、この変数がありません。グローバル関数などを使用する必要がありますか?

function openTab() {
  //Save an address of current page
  path=content.location.href;
  //Open new Tab and select it
  var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
  var newTabBrowser = gBrowser.getBrowserForTab(tab);
  gBrowser.selectedTab=tab;
}

function write() {
  content.body.innerHTML=path;
}

openTab()適切なボタンが押されると、関数が実行されます。機能も同様write()

4

1 に答える 1

1
var path = null;
function openTab() {
  //Save an address of current page
  path=content.location.href;
  //Open new Tab and select it
  var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
  var newTabBrowser = gBrowser.getBrowserForTab(tab);
  gBrowser.selectedTab=tab;
}

function write() {
  if (path!=null)
    content.body.innerHTML=path;
}

---編集 この例は?すべてが xopen にカプセル化されています。

<script type="text/javascript">

    var xopen = function()
    {
        this.path = null;
        this.openTab = function()
        {
            //Save an address of current page
            this.path=content.location.href;
            //Open new Tab and select it
            var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
            var newTabBrowser = gBrowser.getBrowserForTab(tab);
            gBrowser.selectedTab=tab;   
        }

        this.write = function () 
        {
            if (this.path!=null)
                content.body.innerHTML=this.path;
        }
    }
    var wopen = new xopen();

</script>
<button onclick="wopen.openTab()">openTab</button>
<button onclick="wopen.write()">write</button>
于 2009-08-19T21:02:19.600 に答える