Skip to content

Commit

Permalink
reduce hard minimum chrome version to 94 with a warning bar
Browse files Browse the repository at this point in the history
  • Loading branch information
xmcp committed Apr 20, 2024
1 parent 7f93965 commit ffff7b3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 40 deletions.
14 changes: 7 additions & 7 deletions pakkujs/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reset_dnr_status} from "./danmu_update_blocker";
import {install_dnr_rule} from "./danmu_update_blocker";
import {get_config, hotfix_on_update, save_config} from "./config";
import {get_state, HAS_SESSION_STORAGE, init_state, save_state} from "./state";

Expand Down Expand Up @@ -150,23 +150,25 @@ function install_declarative_stuff() {
// best practice to re-install all declarative stuff on every startup
// https://groups.google.com/a/chromium.org/g/chromium-extensions/c/ZM0Vzb_vuIs/m/Nm4gK-X0AQAJ

void reset_dnr_status();
void install_dnr_rule();
void install_context_menu();
void install_content_script();
}

chrome.runtime.onStartup.addListener(async ()=>{
install_declarative_stuff();

if(!HAS_SESSION_STORAGE) {
console.error('pakku state: EMULATING session storage');
await chrome.storage.local.clear();
// redo the init since the state is reset
await perform_init();
}

install_declarative_stuff();
});

chrome.runtime.onInstalled.addListener(async (details)=>{
install_declarative_stuff();

if(details.reason==='install') {
void chrome.tabs.create({url: chrome.runtime.getURL('page/options.html')});
}
Expand All @@ -177,8 +179,6 @@ chrome.runtime.onInstalled.addListener(async (details)=>{
hotfix_on_update(config);
await save_config(config);
}

install_declarative_stuff();
});

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
Expand Down Expand Up @@ -217,7 +217,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
return true;
}
else if(msg.type==='reset_dnr_status') {
void reset_dnr_status();
void install_dnr_rule();
}
else if(msg.type==='xhr_proxy') {
let perform = async ()=>{
Expand Down
9 changes: 6 additions & 3 deletions pakkujs/background/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ export async function save_config<SomeConfig extends Partial<Config>>(config: So
await chrome.storage.sync.set(config);
}

export async function get_config(): Promise<Config> {
let remote = await chrome.storage.sync.get();
return migrate_config(remote);
export function get_config(): Promise<Config> {
return new Promise((resolve)=>{
chrome.storage.sync.get((config: AnyObject)=>{
resolve(migrate_config(config));
});
});
}

function _to_int(config: AnyObject, k: (keyof Config)) {
Expand Down
39 changes: 29 additions & 10 deletions pakkujs/background/danmu_update_blocker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import {get_config} from "./config";
import Rule = chrome.declarativeNetRequest.Rule;

export async function reset_dnr_status() {
const RULE = {
id: 1001,
action: {
type: 'block',
},
condition: {
requestDomains: [
'chat.bilibili.com'
],
excludedInitiatorDomains: [
'live.bilibili.com'
],
resourceTypes: [
'websocket'
],
},
} as Rule;

export async function install_dnr_rule() {
let config = await get_config();
let enabled = (await chrome.declarativeNetRequest.getDynamicRules()).some(r => r.id===RULE.id);

// reset status of danmu-update-blocker
if(config.BREAK_UPDATE) {
await chrome.declarativeNetRequest.updateEnabledRulesets({
enableRulesetIds: ['danmu-update-blocker'],
if(config.BREAK_UPDATE && !enabled) {
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [RULE],
});
console.log('pakku update blocker: enabled dnr ruleset');
} else {
await chrome.declarativeNetRequest.updateEnabledRulesets({
disableRulesetIds: ['danmu-update-blocker'],
console.log('pakku update blocker: add dnr ruleset');
} else if(!config.BREAK_UPDATE && enabled) {
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE.id],
});
console.log('pakku update blocker: disabled dnr ruleset');
console.log('pakku update blocker: remove dnr ruleset');
}
}
27 changes: 18 additions & 9 deletions pakkujs/background/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MessageStats, Stats, int, AnyObject} from "../core/types";
import {MessageStats, Stats, AnyObject} from "../core/types";

// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/session#browser_compatibility
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/setAccessLevel#browser_compatibility
Expand All @@ -22,11 +22,18 @@ export type State = typeof DEFAULT_STATE & {

export async function init_state(): Promise<boolean> {
let store = HAS_SESSION_STORAGE ? chrome.storage.session : chrome.storage.local;
let {_INITIALIZED} = await store.get(['_INITIALIZED']);
let _INITIALIZED = false;
try {
_INITIALIZED = (await store.get(['_INITIALIZED']))._INITIALIZED;
} catch(e) {}

if(!_INITIALIZED) {
console.log('pakku state: init state');

// maybe no permission
if(store.setAccessLevel)
await store.setAccessLevel({accessLevel: 'TRUSTED_AND_UNTRUSTED_CONTEXTS'});

await store.set(DEFAULT_STATE);
return true;
}
Expand All @@ -43,11 +50,13 @@ export async function remove_state(keys: (keyof State)[]) {
await store.remove(keys);
}

export async function get_state(): Promise<State> {
let store = HAS_SESSION_STORAGE ? chrome.storage.session : chrome.storage.local;
let state = await store.get() as State;

if(!state._INITIALIZED)
return DEFAULT_STATE;
return state;
export function get_state(): Promise<State> {
return new Promise((resolve)=>{
let store = HAS_SESSION_STORAGE ? chrome.storage.session : chrome.storage.local;
store.get((state: State)=>{
if(!state._INITIALIZED)
resolve(DEFAULT_STATE);
resolve(state);
});
});
}
8 changes: 7 additions & 1 deletion pakkujs/content_script/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@ function get_player_blacklist(): BlacklistItem[] {
let tabid: null | int = null;
let unreg_userscript = true;

function get_tabid() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({type: 'get_tabid'}, resolve);
});
}

async function apply_local_config(config: Config, is_pure_env: boolean = false): Promise<LocalizedConfig> {
let state = await get_state();

let userscript = config.USERSCRIPT;

if(!tabid) {
tabid = await chrome.runtime.sendMessage({type: 'get_tabid'}) as int;
tabid = await get_tabid() as int;

// storage cleanup
window.onunload = function() {
Expand Down
9 changes: 1 addition & 8 deletions pakkujs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "xmcp",

"manifest_version": 3,
"minimum_chrome_version": "99",
"minimum_chrome_version": "94",

"permissions": [
"notifications",
Expand Down Expand Up @@ -74,13 +74,6 @@
}
],

"declarative_net_request": {
"rule_resources": [{
"id": "danmu-update-blocker",
"enabled": false,
"path": "assets/danmu_update_blocker.json"
}]
},
"web_accessible_resources": [{
"resources": ["/generated/combine_worker.js", "/generated/xhr_hook.js"],
"matches": ["*://*.bilibili.com/*"]
Expand Down
11 changes: 9 additions & 2 deletions pakkujs/page/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {DEFAULT_CONFIG, get_config, migrate_config, save_config} from '../background/config';
import Permissions = chrome.permissions.Permissions;

const IS_FIREFOX = process.env.PAKKU_CHANNEL==='firefox';
const IS_EDG = process.env.PAKKU_CHANNEL==='chrome' && navigator.userAgent.includes('Edg/');
Expand Down Expand Up @@ -95,7 +96,7 @@ async function ver_check() {
if(process.env.PAKKU_CHANNEL==='chrome' && chrome_ver && chrome_ver<MIN_CHROME_VERSION) {
note.style.display = 'initial';
note.href = 'https://www.google.cn/chrome/';
note.textContent = `你的浏览器内核版本不受支持(实为 ${chrome_ver},需要 ${MIN_CHROME_VERSION} 以上)。请更新浏览器。`;
note.textContent = `你的浏览器内核版本太低(实为 ${chrome_ver},需要 ${MIN_CHROME_VERSION}),部分功能不可用。请更新浏览器。`;
}

if(IS_EDG) {
Expand Down Expand Up @@ -133,8 +134,14 @@ for(let elem of document.querySelectorAll('.donate')) {
});
}

function get_perms(): Promise<Permissions> {
return new Promise((resolve)=>{
chrome.permissions.getAll(resolve);
});
}

let config = await get_config();
let perms = await chrome.permissions.getAll();
let perms = await get_perms();

if(!perms.origins?.includes('*://*.bilibili.com/*')) {
id('fix-permission-hint').style.display = 'initial';
Expand Down

0 comments on commit ffff7b3

Please sign in to comment.