myblock/lib/chrome/chrome.js

120 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-07-31 08:03:31 +00:00
var app = {};
chrome.webRequest.onBeforeRequest.addListener(function (e) {
2020-09-02 10:39:21 +00:00
for(var u of ["https://video-", "https://txvid.vod"]) {
if(e.url.startsWith(u)) {
return {"cancel": true};
};
}
}, {"urls": ["*://*/*"]}, ["blocking"]);
2020-07-31 08:03:31 +00:00
app.version = function () {return chrome.runtime.getManifest().version};
app.homepage = function () {return chrome.runtime.getManifest().homepage_url};
app.tab = {"open": function (url) {chrome.tabs.create({"url": url, "active": true})}};
if (!navigator.webdriver) {
chrome.runtime.setUninstallURL(app.homepage() + "?v=" + app.version() + "&type=uninstall", function () {});
chrome.runtime.onInstalled.addListener(function (e) {
chrome.management.getSelf(function (result) {
if (result.installType === "normal") {
window.setTimeout(function () {
var previous = e.previousVersion !== undefined && e.previousVersion !== app.version();
var doupdate = previous && parseInt((Date.now() - config.welcome.lastupdate) / (24 * 3600 * 1000)) > 45;
if (e.reason === "install" || (e.reason === "update" && doupdate)) {
var parameter = (e.previousVersion ? "&p=" + e.previousVersion : '') + "&type=" + e.reason;
app.tab.open(app.homepage() + "?v=" + app.version() + parameter);
config.welcome.lastupdate = Date.now();
}
}, 3000);
}
});
});
}
app.button = {
"clicked": function (callback) {
chrome.browserAction.onClicked.addListener(callback);
},
"icon": function (state) {
chrome.browserAction.setIcon({
"path": {
"16": "../../data/icons/" + (state ? state + '/' : '') + "16.png",
"32": "../../data/icons/" + (state ? state + '/' : '') + "32.png",
"48": "../../data/icons/" + (state ? state + '/' : '') + "48.png",
"64": "../../data/icons/" + (state ? state + '/' : '') + "64.png"
}
});
}
};
app.storage = (function () {
var objs = {};
window.setTimeout(function () {
chrome.storage.local.get(null, function (o) {
objs = o;
var script = document.createElement("script");
script.src = "../common.js";
document.body.appendChild(script);
});
}, 0);
/* */
return {
"read": function (id) {return objs[id]},
"changed": function (callback) {chrome.storage.onChanged.addListener(callback)},
"write": function (id, data) {
var tmp = {};
tmp[id] = data;
objs[id] = data;
chrome.storage.local.set(tmp, function () {});
}
}
})();
app.options = (function () {
var tmp = {};
chrome.runtime.onMessage.addListener(function (request, sender, sendeponse) {
for (var id in tmp) {
if (tmp[id] && (typeof tmp[id] === "function")) {
if (request.path === "options-to-background") {
if (request.method === id) tmp[id](request.data);
}
}
}
});
/* */
return {
"receive": function (id, callback) {tmp[id] = callback},
"send": function (id, data) {
chrome.runtime.sendMessage({"path": "background-to-options", "method": id, "data": data});
}
}
})();
app.content_script = (function () {
var tmp = {};
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
for (var id in tmp) {
if (tmp[id] && (typeof tmp[id] === "function")) {
if (request.path === "page-to-background") {
if (request.method === id) {
tmp[id](request.data);
}
}
}
}
});
/* */
return {
"receive": function (id, callback) {tmp[id] = callback},
"send": function (id, data) {
chrome.tabs.query({}, function (tabs) {
tabs.forEach(function (tab) {
chrome.tabs.sendMessage(tab.id, {"path": "background-to-page", "method": id, "data": data});
});
});
}
}
})();