I've been working on converting a Chrome extension that stopped functioning in manifest version 2. I've removed inline JavaScript and switched from chrome.extension.connect to chrome.runtime.connect. However, I'm still encountering issues with chrome.runtime.connect (which I changed from chrome.extension.connect). Any suggestions?
File 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) { }
}
File 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"
}