2

ウェブページでの名前の出現をすべて変更するChromeの拡張機能を作成しようとしています。(たとえば、ページに「that」という単語が含まれている場合は、別の名前に変更されます)。

アイデアは、ユーザーがブラウザのボタンをクリックしたときにこの変更を加えることです。

私の問題は、それが変更を加えないことです!何が間違っているのかわかりません。

これがmanifest.jsonです:

{
  "name": "App Name",
  "description": "App description",
  "version": "1.0",
  "background": {
    "scripts": ["jquery.min.js", "jquery.ba-replacetext.min.js","background.js"]
  },
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
      "default_title": "App name",
      "default_icon": "icon.png"
  },
  "manifest_version": 2
}

そして、これがbackground.jsです:

chrome.browserAction.onClicked.addListener(function(tab) {
    var state = document.readyState;
    var carregou = false;
    var matches = document.body.innerText.match(regex);

if (state == "interactive"|| (!carregou)){
 $("body *").replaceText( /That/gi, "" );
 var regex = /That/gi;
 matches = document.body.innerText.match(regex);
 if(matches){
    alert("Reload the page (f5)");
 }else{ 
    alert("All changes done! :D");
    carregou = true;
 }
}
});

ブラウザのボタンをクリックせずにページを変更するものを実行しましたが、機能します。コードは次のとおりです。

{
  "name": "App Name",
  "version": "1.0",
  "manifest_version": 2,
  "description": "App description.",
  "content_scripts": [
   {
   "matches": ["http://www.facebook.com/*"],
   "js": ["jquery.min.js","jquery.ba-replacetext.min.js", "script.js"],
   "run_at": "document_end"
   }
   ]
}

sript.js:

var state = document.readyState;
var carregou = false;
var matches = document.body.innerText.match(regex);
if (state == "interactive"|| (!carregou)){
 $("body *").replaceText( /That/gi, "" );
 var regex = /That/gi;
 matches = document.body.innerText.match(regex);
 if(matches){
    alert("Reload the pahe (f5)");
 }else{ 
    alert("All changes done! :D");
    carregou = true;
 }
}

Chromeバージョン:Windows7で23.0.1271.95mありがとうございます!

4

1 に答える 1

2

ケース 1)

background.js は、独自に生成された html ページの世界でコードを実行します。アーキテクチャの概要で説明されているように、バックグラウンド ページは、拡張プロセスで実行される HTML ページです。拡張機能の存続期間中存在し、一度にアクティブになるインスタンスは 1 つだけです。

したがって、すべてのコードは、background.htmlページ内の名前のすべての出現を変更しようとしています

同じコードを使用して、プログラムによるインジェクションで機能を実現できます

デモンストレーション

/* in background.html */
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null,
                           {code:"document.body.bgColor='red'"});
});

ケース 2)

Programmatic Injection"matches": ["http://www.facebook.com/*"],の代わりにすべての facebook URL のスクリプトを完全に注入したため、manifest.json にブラウザー アクションがないためではなく、機能しました。

{
  "name": "App Name",
  "version": "1.0",
  "manifest_version": 2,
  "description": "App description.",
  "content_scripts": [
   {
   "matches": ["http://www.facebook.com/*"],
   "js": ["jquery.min.js","jquery.ba-replacetext.min.js", "script.js"],
   "run_at": "document_end"
   }
   ]
}

さらに情報が必要な場合はお知らせください。

于 2012-12-03T03:18:13.573 に答える