generate stylus-firefox.zip without declarativeContent

This commit is contained in:
tophf 2020-10-19 17:43:29 +03:00
parent 818f6313c6
commit 1d1d75d2ac
4 changed files with 37 additions and 35 deletions

View File

@ -45,11 +45,8 @@ CHROME && (async () => {
actions: [
new chrome.declarativeContent.RequestContentScript({
allFrames: true,
js: [
'/content/style-via-xhr.js',
// This sets them to run earlier than document_start
...chrome.runtime.getManifest().content_scripts[0].js,
],
// This runs earlier than document_start
js: chrome.runtime.getManifest().content_scripts[0].js,
}),
],
}]);

View File

@ -57,13 +57,25 @@ self.INJECTED !== 1 && (() => {
if (STYLE_VIA_API) {
await API.styleViaAPI({method: 'styleApply'});
} else {
const styles = Array.isArray(window.STYLES) && window.STYLES[0] ||
await API.getSectionsByUrl(getMatchUrl());
delete window.STYLES;
const styles = chrome.app && getStylesViaXhr() || await API.getSectionsByUrl(getMatchUrl());
await styleInjector.apply(styles);
}
}
function getStylesViaXhr() {
if (new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).test(document.cookie)) {
const url = 'blob:' + chrome.runtime.getURL(RegExp.$2);
const xhr = new XMLHttpRequest();
document.cookie = `${chrome.runtime.id}=1; max-age=0`; // remove our cookie
try {
xhr.open('GET', url, false); // synchronous
xhr.send();
URL.revokeObjectURL(url);
return JSON.parse(xhr.response);
} catch (e) {}
}
}
function getMatchUrl() {
let matchUrl = location.href;
if (!chrome.tabs && !matchUrl.match(/^(http|file|chrome|ftp)/)) {

View File

@ -1,18 +0,0 @@
'use strict';
// eslint-disable-next-line no-unused-expressions
self.INJECTED !== 1 &&
!(document instanceof XMLDocument && !chrome.app) && // this is STYLE_VIA_API
new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).test(document.cookie) &&
(() => {
const url = 'blob:' + chrome.runtime.getURL(RegExp.$2);
const xhr = new XMLHttpRequest();
document.cookie = `${chrome.runtime.id}=1; max-age=0`;
try {
xhr.open('GET', url, false); // synchronous
xhr.send();
// guarding against an implicit global variable for a DOM element with id="STYLES"
window.STYLES = [JSON.parse(xhr.response)];
URL.revokeObjectURL(url);
} catch (e) {}
})();

View File

@ -3,10 +3,11 @@
const fs = require('fs');
const archiver = require('archiver');
const manifest = require('../manifest.json');
function createZip() {
const fileName = 'stylus.zip';
const exclude = [
function createZip({isFirefox} = {}) {
const fileName = `stylus${isFirefox ? '-firefox' : ''}.zip`;
const ignore = [
'.*', // dot files/folders (glob, not regexp)
'vendor/codemirror/lib/**', // get unmodified copy from node_modules
'node_modules/**',
@ -38,15 +39,25 @@ function createZip() {
});
archive.pipe(file);
archive.glob('**', {ignore: exclude});
if (isFirefox) {
const name = 'manifest.json';
const keyOpt = 'optional_permissions';
ignore.unshift(name);
manifest[keyOpt] = manifest[keyOpt].filter(p => p !== 'declarativeContent');
if (!manifest[keyOpt].length) {
delete manifest[keyOpt];
}
archive.append(JSON.stringify(manifest, null, ' '), {name, stats: fs.lstatSync(name)});
}
archive.glob('**', {ignore});
// Don't use modified codemirror.js (see "update-libraries.js")
archive.directory('node_modules/codemirror/lib', 'vendor/codemirror/lib');
archive.finalize();
});
}
createZip()
.then(() => console.log('\x1b[32m%s\x1b[0m', 'Stylus zip complete'))
.catch(err => {
throw err;
});
(async () => {
await createZip();
await createZip({isFirefox: true});
console.log('\x1b[32m%s\x1b[0m', 'Stylus zip complete');
})();