// ==UserScript==
// @name 禁止访问网页
// @name:en Be A Good Guy | Block Webites
// @name:zh-cn 禁止访问网页
// @description 禁止访问网页列表,做一个自律的人,成为更好的自己。
// @description:en Block websites, be a disciplined person, and become a better self.
// @description:zh-cn 禁止访问网页列表,做一个自律的人,成为更好的自己。
// @namespace https://github.com/BasilGuo/
// @license MIT
// @version 0.1.3
// @author Basil Guo
// @match http*://*/*
// @grant GM_addStyle
// @grant GM_deleteValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @icon https://images2.imgbox.com/01/e3/5as6gVyM_o.png
// @homepageURL https://gist.github.com/BasilGuo/64c0730f2cdee29d2abf057577b86b39
// @run-at document-start
// @downloadURL https://update.greasyfork.icu/scripts/469409/%E7%A6%81%E6%AD%A2%E8%AE%BF%E9%97%AE%E7%BD%91%E9%A1%B5.user.js
// @updateURL https://update.greasyfork.icu/scripts/469409/%E7%A6%81%E6%AD%A2%E8%AE%BF%E9%97%AE%E7%BD%91%E9%A1%B5.meta.js
// ==/UserScript==
(function () {
'use strict';
// Your code here...
var g_one_rule_window = false;
const G_LANGUAGE_INDEX = ('en' === navigator.language || 'en' === navigator.userLanguage) ? 0 : 1;
// https://stackoverflow.com/questions/55935641/how-to-get-language-of-user-navigator-languages-not-working
// console.log(navigator.language, navigator.userLanguage);
// console.log(navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage));
const G_RULE_PROMPT = {
'title': {
'addRule': ["Add A Rule For Current Site", '为本站添加规则'],
'listRule': ["List Rules of All Sites", '列出所有网站规则'],
'clearRule': ["Clear All Rules of All Sites", "清空所有网站规则"],
'customURL': ["Custom the Redirect Webite", "自定义重定向网站"]
},
'common': {
'btnCancelText': [`Cancel`, `取消`],
'btnOKText': [`OK`, `确认`],
'btnDeleteText': [`Delete`, `删除`],
'noRuleText': [`There is no access rules now.`, `当前没有受限访问网站`],
'alertText': [`There is a rule window opened. Please close it first~`, `已经有一个规则窗口了,请先关闭吧~`],
'redirectURLText': [`Input the URL you would finally accessed when browsing blocked websites`, `输入访问被禁止网站时,最终重定向到的网址`]
},
'add': {
'titleText': [`Add access time limit rule for
${location.host}`, `给${location.host}
添加访问限制时间规则`],
'siteURLText': [`Set the site path. Default is the current hostname so you could let it alone. Not support for wildcards now.`, `设置本网站屏蔽规则,暂不支持通配符,默认为当前网站域名,非必填项`],
'limitDayLongText': [`Limit for all day`, `全天限制`],
'limitStartTimeText': [`Limit start time: `, `限制开始时间`],
'limitEndTimeText': [`Limit end time: `, `限制结束时间`],
'failedTimeText': [`Add rule failed for limit start time should be less than end time`, `添加规则失败:限制开始时间应该先于限制结束时间`],
'failedHostText': [`Add rule failed because URL of current site is wrong, please correct it.
Take the following www.example.com/path/subpath as an example. No protocols is needed.`, `添加规则失败:网站URL规则设置错误,参考例子不带协议`],
'successText': [`Add rule success, effect after refress site.`, `规则添加成功,刷新页面以生效规则`]
},
'list': {
'titleText': [`Limit access rules for all websites`, `当前所有网站限制访问规则`],
'ruleHostText': [`Limit Website`, `限制网站`],
'ruleTimeStartText': [`Limit Start Time`, `限制开始时间`],
'ruleTimeEndText': [`Limit End Time`, `限制结束时间`],
'deleteActionText': [`Actions`, `操作`],
'deleteRuleConfirmText1': [`Are you sure to delete?`, `真的要删除限制规则吗?`],
'deleteRuleConfirmText2': [`Buddy, being the best self?`, `你不打算成为更好的自己了吗?`],
'deleteRuleConfirmText3': [`OK, the last time to confirm.`, `好吧,最后一次确认了。`],
'deleteRuleCancelText': [`OK, I have known you are the best.`, `我就知道你是最棒的!`],
}
};
function cancelRuleWindow() {
let rule_window = document.getElementById('bs-rule');
rule_window.parentNode.removeChild(rule_window);
g_one_rule_window = false;
}
function addRuleLimitTimeDaylongOnChange() {
let limit_time_daylong_cbx = document.getElementById('rule-time-day-long');
let limit_time_start_input = document.getElementById('rule-time-start');
let limit_time_end_input = document.getElementById('rule-time-end');
if (limit_time_daylong_cbx.checked) {
limit_time_start_input.disabled = "disabled";
limit_time_end_input.disabled = "disabled";
}
else {
limit_time_start_input.disabled = "";
limit_time_end_input.disabled = "";
}
}
function addRuleOKButtonClick() {
let time_arr, hour, minute;
let limit_start_secs, limit_end_secs;
let limit_time_daylong_cbx = document.getElementById('rule-time-day-long');
let limit_time_start = document.getElementById('rule-time-start').value;
let limit_time_end = document.getElementById('rule-time-end').value;
let rule_url_input = document.getElementById('rule-url');
if (limit_time_daylong_cbx.checked) {
limit_time_start = '00:00';
limit_time_end = '23:59';
}
time_arr = limit_time_start.split(':');
hour = time_arr[0];
minute = time_arr[1];
limit_start_secs = hour * 3600 + minute * 60;
time_arr = limit_time_end.split(':');
hour = time_arr[0];
minute = time_arr[1];
limit_end_secs = hour * 3600 + minute * 60;
if (limit_start_secs > limit_end_secs) {
alert(G_RULE_PROMPT.add.failedTimeText[G_LANGUAGE_INDEX]);
return;
} else {
let rule_url = rule_url_input.value;
let all_hosts = GM_getValue('BlockWebsitesRules');
let all_hosts_set;
if (rule_url === '') {
rule_url = document.location.host;
} else if (rule_url.indexOf("://") !== -1) {
rule_url = rule_url.split("://")[1];
}
rule_url = rule_url.replace(/\/$/g, ''); // remove the last '/'
if (all_hosts === undefined || all_hosts === '') {
all_hosts_set = new Set();
} else {
all_hosts_set = new Set(JSON.parse(all_hosts)['BlockWebsitesRules'].split(' '));
}
if (document.location.href.indexOf(rule_url) === -1 || rule_url.indexOf(document.location.host) === -1) {
alert(G_RULE_PROMPT.add.failedHostText[G_LANGUAGE_INDEX]);
return;
}
console.log("all_hosts: ", all_hosts);
all_hosts_set.add(rule_url);
all_hosts = { 'BlockWebsitesRules': Array.from(all_hosts_set).join(' ') };
console.log('BlockWebsitesRules: ', all_hosts);
GM_setValue(rule_url, JSON.stringify({ 'start': limit_start_secs, 'end': limit_end_secs }));
GM_setValue("BlockWebsitesRules", JSON.stringify(all_hosts));
cancelRuleWindow();
alert(G_RULE_PROMPT.add.successText[G_LANGUAGE_INDEX]);
location.reload();
}
}
function addRuleActions() {
let limit_time_daylong_cbx = document.getElementById('rule-time-day-long');
let ok_add_btn = document.getElementById("ok-add-btn");
let cancel_add_btn = document.getElementById("cancel-add-btn");
limit_time_daylong_cbx.onchange = addRuleLimitTimeDaylongOnChange;
ok_add_btn.onclick = addRuleOKButtonClick;
cancel_add_btn.onclick = cancelRuleWindow;
g_one_rule_window = true;
}
function addRuleWindow() {
let add_rule_div = `
${G_RULE_PROMPT.list.ruleHostText[G_LANGUAGE_INDEX]} | ${G_RULE_PROMPT.list.ruleTimeStartText[G_LANGUAGE_INDEX]} | ${G_RULE_PROMPT.list.ruleTimeEndText[G_LANGUAGE_INDEX]} | ${G_RULE_PROMPT.list.deleteActionText[G_LANGUAGE_INDEX]} |