2016-06-01 19 views
0

Google Chromeの簡単な拡張機能を試していますが、間違いがあると思います。この拡張は、他の単語の単語を置き換えます。一部のユーザーは、同様の質問を持っているが、私は私の問題への解決策を見つけるcan't:$Chrome拡張単語置換

これは、HTML

ある
<button id="iniciar">Cambiar</button> 
    <script type="text/javascript" src=cambiar.js></script> 

これは

document.addEventListener('DOMContentLoaded', function() { 
    "use strict"; 
    var palabra = document.getElementById('iniciar'); 

    palabra.addEventListener("click", function() { 

     document.body.innerHTML = document.body.innerHTML.replace(new RegExp("1", 'g'), "2"); 

    }); 
}); 

そして、このJSONをJS

{ 
    "name": "TextBreak" 
    , "version": "0.1" 
    , "manifest_version": 2 
    , "browser_action": { 
     "default_icon": "flag.png" 
     , "default_title": "TextBreak" 
     , "default_popup": "popup.html" 
    } 
    , "content_scripts": [ 
     { 
      "matches": ["http://*/*", "https://*/*"] 
      , "js": ["cambiar.js"] 
      , "run_at": "document_end" 
     } 
    ] 
} 
+0

あなたの質問は何ですか?バグの詳細は何ですか? – Kelvin

+0

プラグインをインストールしてWebページでテストすると、1から2に変更されません。 –

+0

コンソールにエラー情報がありますか? – Kelvin

答えて

0

あなたのコードの全体の概念は間違っています。あなたはまだ基本を理解していないようです。私はさらに実験する前にarchitecture of Chrome extensionsについて読むことをお勧めします。あなたのコード内

エラー:

  1. Content scripts are executed on the page that they are injected into。つまり、ポップアップではなく、ターゲットページの"iniciar"要素に関数を接続しようとしています。

  2. あなたのコンテンツスクリプトは、決してあなたのポップアップページに接続されていません。それは、一致するすべてのページに自動で注入されます。ポップアップからコンテンツスクリプトの挿入を開始する場合は、programmatic injectionを使用する必要があります。

  3. "run_at": "document_end"のコンテンツスクリプトは、DOMの作成後に挿入されます。つまり、あなたのDOMContentLoadedリスナーは決して発射されません。

0

ありがとうございます、今すぐ拡張機能が動作します。

HTML。

<button id="iniciar">Cambiar</button> 
<script type="text/javascript" src="cambiar.js"></script> 

JS

document.addEventListener('DOMContentLoaded', function() { 
    var detectado = document.getElementById('iniciar'); 
     detectado.addEventListener('click', function() { 
     chrome.tabs.executeScript(null, {code: "document.body.innerHTML = document.body.innerHTML.replace(new RegExp('1' , 'g'), '2');"});  

    }); 
}); 

JSON

"permissions": [ 
     "tabs", 
     "<all_urls>" 
    ] 
    , "browser_action": { 
     "default_icon": "flag.png" 
     , "default_title": "Relaxing cup of café con leche" 
     , "default_popup": "popup.html" 
    }, 
    "content_scripts": [ 
     { 
      "matches": [ 
       "<all_urls>" 
      ], 
      "js": ["cambiar.js"] 
     } 
    ] 
関連する問題