Commit f8b4bd37 authored by Manish Jethani's avatar Manish Jethani

Issue 5028 - Use browser namespace

parent d800fb3d
......@@ -18,6 +18,7 @@ Directory structure
page, should *not be imported*.
* `desktop-options.html`, `desktop-options.js`: Options page, see below
* `subscriptions.xml`: Test subscription data, should *not be imported*
* `polyfill.js`: Browser API polyfills, should *not be imported*
* `locale` directory: Localized strings, with one directory per locale. The
Firefox format for locale identifiers is used (xx-YY where xx is the language
code and YY the optional region code). The localization strings themselves are
......
......@@ -19,6 +19,7 @@
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="polyfill.js"></script>
<script type="text/javascript" src="ext/common.js"></script>
<script type="text/javascript" src="ext/background.js"></script>
<script type="text/javascript" src="background.js"></script>
......
......@@ -92,7 +92,7 @@
},
get appLocale()
{
return chrome.i18n.getUILanguage();
return browser.i18n.getUILanguage();
},
get readingDirection()
{
......
......@@ -26,7 +26,7 @@ function E(id)
function getDocLink(link, callback)
{
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "app.get",
what: "doclink",
link
......@@ -60,7 +60,7 @@ function setLinks(id, ...args)
function checkShareResource(url, callback)
{
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "filters.blocked",
url,
requestType: "SCRIPT",
......
......@@ -22,6 +22,7 @@
<title class="i18n_options_page_title"></title>
<link rel="stylesheet" href="skin/common.css">
<link rel="stylesheet" href="skin/desktop-options.css">
<script src="polyfill.js"></script>
<script src="ext/common.js"></script>
<script src="ext/content.js"></script>
<script src="common.js"></script>
......
......@@ -27,7 +27,7 @@
let acceptableAdsUrl = null;
let acceptableAdsPrivacyUrl = null;
let isCustomFiltersLoaded = false;
let {getMessage} = chrome.i18n;
let {getMessage} = browser.i18n;
let customFilters = [];
let filterErrors = new Map([
["synchronize_invalid_url",
......@@ -543,7 +543,7 @@
function sendMessageHandleErrors(message, onSuccess)
{
chrome.runtime.sendMessage(message, (errors) =>
browser.runtime.sendMessage(message, (errors) =>
{
if (errors.length > 0)
alert(errors.join("\n"));
......@@ -585,11 +585,11 @@
let subscriptionType = subscription.recommended;
if (subscriptionType == "ads" && subscription.disabled == false)
{
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.remove",
url: subscription.url
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.add",
url: findParentData(element, "access", false)
});
......@@ -627,13 +627,13 @@
break;
}
case "remove-filter":
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "filters.remove",
text: findParentData(element, "access", false)
});
break;
case "remove-subscription":
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.remove",
url: findParentData(element, "access", false)
});
......@@ -654,12 +654,12 @@
break;
case "switch-acceptable-ads":
let value = element.value || element.dataset.value;
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: value == "privacy" ? "subscriptions.add" :
"subscriptions.remove",
url: acceptableAdsPrivacyUrl
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: value == "ads" ? "subscriptions.add" : "subscriptions.remove",
url: acceptableAdsUrl
});
......@@ -668,14 +668,14 @@
switchTab(element.getAttribute("href").substr(1));
break;
case "toggle-disable-subscription":
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.toggle",
keepInstalled: true,
url: findParentData(element, "access", false)
});
break;
case "toggle-pref":
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "prefs.toggle",
key: findParentData(element, "pref", false)
});
......@@ -684,7 +684,7 @@
let subscriptionUrl = findParentData(element, "access", false);
if (element.getAttribute("aria-checked") == "true")
{
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.remove",
url: subscriptionUrl
});
......@@ -693,12 +693,12 @@
addEnableSubscription(subscriptionUrl);
break;
case "update-all-subscriptions":
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.update"
});
break;
case "update-subscription":
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.update",
url: findParentData(element, "access", false)
});
......@@ -860,7 +860,7 @@
populateLists();
// Initialize navigation sidebar
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "app.get",
what: "addonVersion"
},
......@@ -909,7 +909,7 @@
onPrefMessage(key, value, true);
});
}
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "app.get",
what: "features"
},
......@@ -1065,7 +1065,7 @@
collections[property].clearAll();
setCustomFiltersView("empty");
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.get",
special: true
},
......@@ -1074,7 +1074,7 @@
// Load filters
for (let subscription of subscriptions)
{
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "filters.get",
subscriptionUrl: subscription.url
},
......@@ -1085,7 +1085,7 @@
}
});
loadRecommendations();
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "prefs.get",
key: "subscriptions_exceptionsurl"
},
......@@ -1093,7 +1093,7 @@
{
acceptableAdsUrl = url;
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "prefs.get",
key: "subscriptions_exceptionsurl_privacy"
},
......@@ -1102,7 +1102,7 @@
acceptableAdsPrivacyUrl = urlPrivacy;
// Load user subscriptions
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.get",
downloadable: true
},
......@@ -1160,7 +1160,7 @@
if (homepage)
message.homepage = homepage;
chrome.runtime.sendMessage(message);
browser.runtime.sendMessage(message);
}
function onFilterMessage(action, filter)
......@@ -1271,7 +1271,7 @@
return;
}
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "prefs.get",
key
}, callback);
......@@ -1365,20 +1365,20 @@
}
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "app.listen",
filter: ["addSubscription", "focusSection"]
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "filters.listen",
filter: ["added", "loaded", "removed"]
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "prefs.listen",
filter: ["notifications_ignoredcategories", "notifications_showui",
"show_devtools_panel", "shouldShowBlockElementMenu"]
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.listen",
filter: ["added", "disabled", "homepage", "lastDownload", "removed",
"title", "downloadStatus", "downloading"]
......
......@@ -20,6 +20,7 @@
<head>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="skin/devtools-panel.css">
<script src="polyfill.js"></script>
<script src="ext/common.js"></script>
<script src="ext/content.js"></script>
<script src="ext/devtools.js"></script>
......
......@@ -19,7 +19,7 @@
let lastFilterQuery = null;
chrome.runtime.sendMessage({type: "types.get"},
browser.runtime.sendMessage({type: "types.get"},
(filterTypes) =>
{
let filterTypesElem = document.getElementById("filter-type");
......@@ -67,7 +67,7 @@ function createActionButton(action, label, filter)
button.addEventListener("click", () =>
{
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "filters." + action,
text: filter
});
......@@ -254,6 +254,6 @@ document.addEventListener("DOMContentLoaded", () =>
// Since Chrome 54 the themeName is accessible, for earlier versions we must
// assume the default theme is being used.
// https://bugs.chromium.org/p/chromium/issues/detail?id=608869
let theme = chrome.devtools.panels.themeName || "default";
let theme = browser.devtools.panels.themeName || "default";
document.body.classList.add(theme);
}, false);
......@@ -19,9 +19,6 @@
(function()
{
if (typeof chrome == "undefined")
window.chrome = {};
if (typeof ext == "undefined")
window.ext = {};
......@@ -77,121 +74,4 @@
window.removeEventListener("message", listener._extWrapper, false);
}
};
/* I18n */
let getLocaleCandidates = function(selectedLocale)
{
let candidates = [];
let defaultLocale = "en_US";
// e.g. "ja-jp-mac" -> "ja_JP", note that the part after the second
// dash is dropped, since we only support language and region
let parts = selectedLocale.split("-");
let language = parts[0];
let region = (parts[1] || "").toUpperCase();
if (region)
candidates.push(language + "_" + region);
candidates.push(language);
if (candidates.indexOf(defaultLocale) == -1)
candidates.push(defaultLocale);
return candidates;
};
let selectedLocale = window.navigator.language;
let match = /[?&]locale=([\w-]+)/.exec(window.location.search);
if (match)
selectedLocale = match[1];
let locales = getLocaleCandidates(selectedLocale);
let catalog = Object.create(null);
let catalogFile = window.location.pathname.replace(/.*\//, "")
.replace(/\..*/, "") + ".json";
let replacePlaceholder = function(text, placeholder, content)
{
return text.split("$" + placeholder + "$").join(content || "");
};
let parseMessage = function(rawMessage)
{
let text = rawMessage.message;
let placeholders = [];
for (let placeholder in rawMessage.placeholders)
{
let {content} = rawMessage.placeholders[placeholder];
if (/^\$\d+$/.test(content))
placeholders[parseInt(content.substr(1), 10) - 1] = placeholder;
else
text = replacePlaceholder(text, placeholder, content);
}
return [text, placeholders];
};
let readCatalog = function(locale, file)
{
let xhr = new XMLHttpRequest();
xhr.open("GET", "locale/" + locale + "/" + file, false);
xhr.overrideMimeType("text/plain");
try
{
xhr.send();
}
catch (e)
{
return;
}
if (xhr.status != 200 && xhr.status != 0)
return;
let rawCatalog = JSON.parse(xhr.responseText);
for (let msgId in rawCatalog)
{
if (!(msgId in catalog))
catalog[msgId] = parseMessage(rawCatalog[msgId]);
}
};
chrome.i18n = {
getUILanguage()
{
return locales[0].replace(/_/g, "-");
},
getMessage(msgId, substitutions)
{
while (true)
{
let message = catalog[msgId];
if (message)
{
let text = message[0];
let placeholders = message[1];
if (!(substitutions instanceof Array))
substitutions = [substitutions];
for (let i = 0; i < placeholders.length; i++)
text = replacePlaceholder(text, placeholders[i], substitutions[i]);
return text;
}
if (locales.length == 0)
return "";
let locale = locales.shift();
readCatalog(locale, "common.json");
readCatalog(locale, catalogFile);
}
}
};
}());
......@@ -61,10 +61,10 @@
/* Polyfills */
if (!("runtime" in chrome))
chrome.runtime = {};
if (!("runtime" in browser))
browser.runtime = {};
chrome.runtime.sendMessage = (message, responseCallback) =>
browser.runtime.sendMessage = (message, responseCallback) =>
{
let messageId = ++maxMessageId;
......
......@@ -23,6 +23,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link type="text/css" href="skin/common.css" rel="stylesheet" />
<link type="text/css" href="skin/firstRun.css" rel="stylesheet"/>
<script type="text/javascript" src="polyfill.js"></script>
<script type="text/javascript" src="ext/common.js"></script>
<script type="text/javascript" src="ext/content.js"></script>
<script type="text/javascript" src="common.js"></script>
......
......@@ -55,7 +55,7 @@
setLinks("share-headline", link);
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "app.get",
what: "issues"
}, (issues) =>
......@@ -77,7 +77,7 @@
updateSocialLinks();
}
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.listen",
filter: ["added", "removed", "updated", "disabled"]
});
......@@ -114,7 +114,7 @@
function openFilters()
{
chrome.runtime.sendMessage({type: "app.open", what: "options"});
browser.runtime.sendMessage({type: "app.open", what: "options"});
}
document.addEventListener("DOMContentLoaded", onDOMLoaded, false);
......
......@@ -21,7 +21,7 @@
// requires messaging the background page. For Chrome and Safari,
// we could get the UI locale here, but would need to duplicate
// the logic implemented in Utils.appLocale.
chrome.runtime.sendMessage(
browser.runtime.sendMessage(
{
type: "app.get",
what: "localeInfo"
......@@ -58,7 +58,7 @@ ext.i18n = {
while (element.lastChild)
element.removeChild(element.lastChild);
processString(chrome.i18n.getMessage(stringName, args), element);
processString(browser.i18n.getMessage(stringName, args), element);
}
};
......
......@@ -29,8 +29,8 @@ exports.initAntiAdblockNotification = function initAntiAdblockNotification()
let notification = {
id: "antiadblock",
type: "question",
title: chrome.i18n.getMessage("notification_antiadblock_title"),
message: chrome.i18n.getMessage("notification_antiadblock_message"),
title: browser.i18n.getMessage("notification_antiadblock_title"),
message: browser.i18n.getMessage("notification_antiadblock_message"),
urlFilters: []
};
......
......@@ -21,6 +21,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" type="text/css" href="skin/mobile-options.css">
<script src="polyfill.js"></script>
<script src="ext/common.js"></script>
<script src="ext/content.js"></script>
<script src="common.js"></script>
......
......@@ -20,7 +20,7 @@
"use strict";
{
const {getMessage} = chrome.i18n;
const {getMessage} = browser.i18n;
const dialogSubscribe = "subscribe";
const idAcceptableAds = "acceptableAds";
......@@ -76,7 +76,7 @@
{
return new Promise((resolve, reject) =>
{
chrome.runtime.sendMessage(
browser.runtime.sendMessage(
{type: "subscriptions.get", downloadable: true},
resolve
);
......@@ -87,7 +87,7 @@
{
return new Promise((resolve, reject) =>
{
chrome.runtime.sendMessage(
browser.runtime.sendMessage(
{type: "prefs.get", key: "subscriptions_exceptionsurl"},
resolve
);
......@@ -117,12 +117,12 @@
function installSubscription(url, title)
{
chrome.runtime.sendMessage({type: "subscriptions.add", url, title});
browser.runtime.sendMessage({type: "subscriptions.add", url, title});
}
function uninstallSubscription(url)
{
chrome.runtime.sendMessage({type: "subscriptions.remove", url});
browser.runtime.sendMessage({type: "subscriptions.remove", url});
}
/* Actions */
......@@ -290,7 +290,7 @@
{
if (whitelistFilter)
{
chrome.runtime.sendMessage(
browser.runtime.sendMessage(
{
type: (toggle.checked) ? "filters.remove" : "filters.add",
text: whitelistFilter
......@@ -413,17 +413,17 @@
}
ext.onMessage.addListener(onMessage);
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "app.listen",
filter: ["addSubscription", "showPageOptions"]
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "filters.listen",
filter: ["added", "removed"]
});
chrome.runtime.sendMessage({
browser.runtime.sendMessage({
type: "subscriptions.listen",
filter: ["added", "disabled", "removed", "title"]
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment