消息传递(chrome.runtime.connect())到扩展失败,chrome manifest版本2

时间:2014-01-18 07:24:41

标签: javascript json google-chrome-extension manifest

我正在尝试转换已停止在清单版本2中工作的Chrome扩展程序,我已取出内联javascript,并将chrome.extension.connect更改为chrome.runtime.connect,但Chrome似乎仍然存在问题.runtime.connect(我从chrome.extension.connect改了),有什么想法吗?

文件ContentScript.js

OnDocumentEnd();

var TranslitOn;
var ToggleKeyCode;
var LocalEnabled;
var port;

var ChangeListener = function (event) { OnChange(event); };

var KeyUpListener = function (event) { OnKeyUp(event, event.target); };
var KeyDownListener = function (event) { OnKeyDown(event); };
var KeyPressListener = function (event) { OnKeyPress(event, event.target); };


function OnDocumentEnd() {
    console.log("ode");

    port = chrome.runtime.connect();

    port.onMessage.addListener(function (msg) {
    console.log("port on msg");
        if (msg.Status != null) {
    console.log("1:"+msg.Status);
   //        TranslitOn = msg.Status;
        }
        if (msg.ToggleKey != null) {
    console.log("2:"+msg.ToggleKey);
           ToggleKeyCode = msg.ToggleKey;
        }
        if (msg.IsEnabled != null) {
    console.log("3:"+msg.IsEnabled);
           LocalEnabled = msg.IsEnabled;
            if (LocalEnabled) {
                Bootstrap();
            }
        }
    });
    port.postMessage({ GetEnabled: true });
    }

file translit.js

<script language="JavaScript" type="text/javascript" src="/import/jquery-2.0.3.min.js"></script>
<script language="JavaScript" type="text/javascript" src="/import/sha1Hash.js"></script>
<script language="JavaScript" type="text/javascript" src="/import/ArrayList.js"></script>
  <script type="text/javascript">


    if (localStorage.getItem('t_hotkey') == null) {
        localStorage.setItem('t_hotkey', 113);
    }

    var ToggleKeyCode = localStorage.getItem('t_hotkey');


    var TranslitOn = false;

    var GlobalEnabled = true;
    var Ports = new ArrayList();

    function FireOnEvent() {
        try {
            chrome.tabs.getSelected(null, function (tab) {
                chrome.tabs.sendRequest(tab.id, { SetStatus: TranslitOn });
                chrome.tabs.sendRequest(tab.id, { ToggleKey: ToggleKeyCode });
            });
        } catch (e) { }
    }

    function Toggle() {
        TranslitOn = !TranslitOn;
        FireOnEvent();
    }

    function UpdateToggleKey() {
        FireOnEvent();
    }

    function Save() {
        try {
            localStorage["ToggleKey"] = ToggleKeyCode;
            localStorage["Enabled"] = GlobalEnabled;
        }
        catch (e) { }
    }

function Init() {
    try {
        chrome.runtime.onConnect.addListener(function (port) {
          port.onMessage.addListener(function (msg) {
            if (msg.GetStatus != null) {
              port.postMessage({ Status: TranslitOn });
            }
            else if (msg.GetToggleKey != null) {
              port.postMessage({ ToggleKey: ToggleKeyCode });
            }
            else if (msg.GetEnabled != null) {
              port.postMessage({ IsEnabled: GlobalEnabled });
            }
          });

          Ports.Add(port);
        });

        chrome.browserAction.onClicked.addListener(function (tab) { Toggle();});

        chrome.runtime.onRequest.addListener(function (request, sender, sendResponse) {
          try {

            if (request.SetStatus != null) {
              if (request.SetStatus != TranslitOn) {
            Toggle();
              }
            }

            if (request.ToggleStatus != null) {
              Toggle();
            }

            if (request.SetToggleKey != null) {
              ToggleKeyCode = request.SetToggleKey;
              UpdateToggleKey();
            }

            if (request.SetEnabled != null) {
              GlobalEnabled = request.SetEnabled;
            }

          }
          catch (e) { }
        });
    } catch (e) { }
}

文件manifest.json

{
   "background": {
   "scripts":["translit.js"],
   "persistent": false},
   "content_scripts": [ {
      "all_frames": true,
      "js": [ "/import/jquery-2.0.3.min.js", "/code/ContentScript.js" ],
      "matches": [ "http://*/*", "https://*/*" ],
      "run_at": "document_end"
   } ],
   "description": "xxxxxxxxxxt",
   "icons": {
      "128": "img/Icon128.png",
      "32": "img/Icon32.png",
      "48": "img/Icon48.png"
   },
   "key": "MIIBIjAxxxxxxxxB",
   "manifest_version": 2,
   "name": "xxxxxxxxxxxxx",
   "permissions": [ "storage", "tabs", "http://*/*", "https://*/*" ],
   "short_name": "xxx",
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "1.23"

}

1 个答案:

答案 0 :(得分:1)

您仍然在translit.js的顶部有HTML代码。删除它并将这些链接添加到Javascript文件到清单中的后台脚本js数组,你应该好好去:)