mirror of
https://github.com/huhu/rust-search-extension
synced 2024-11-15 16:07:57 +00:00
39 lines
No EOL
1.3 KiB
JavaScript
39 lines
No EOL
1.3 KiB
JavaScript
function Compat() {
|
|
this.isChrome = navigator.userAgent.toLowerCase().indexOf("chrome") !== -1;
|
|
this.browser = this.isChrome ? window.chrome : window.browser;
|
|
|
|
// Firefox doesn't support tags in search suggestion.
|
|
this.tagged = this.isChrome ?
|
|
(tag, str) => `<${tag}>${str}</${tag}>` :
|
|
(_, str) => str;
|
|
this.match = (str) => this.tagged("match", str);
|
|
this.dim = (str) => this.tagged("dim", str);
|
|
}
|
|
|
|
// Escape the five predefined entities to display them as text.
|
|
Compat.prototype.escape = function(str) {
|
|
str = str || "";
|
|
return this.isChrome ? str
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'")
|
|
: str;
|
|
};
|
|
|
|
// Compatibly get extension's background page.
|
|
Compat.prototype.getBackgroundPage = function() {
|
|
return this.browser.extension.getBackgroundPage();
|
|
};
|
|
|
|
Compat.prototype.normalizeDate = function(date) {
|
|
let month = '' + (date.getMonth() + 1),
|
|
day = '' + date.getDate(),
|
|
year = date.getFullYear();
|
|
return [year, month.padStart(2, "0"), day.padStart(2, "0")].join('-');
|
|
};
|
|
|
|
Compat.prototype.sendMessage = function(message, response) {
|
|
c.browser.runtime.sendMessage(message, response);
|
|
}; |