Skip to content

Commit

Permalink
10x performance boosts
Browse files Browse the repository at this point in the history
  • Loading branch information
painty committed Jun 9, 2019
1 parent 28874ba commit 675770b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ node_modules
# built
dest/asset/js/content.js
test/index.build.js
test/index.build.js.map
.cache
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
**ver 2.4.0 | 06/09/2019**

1. Code optimization: 10x speed up!
2. Build tool: switch browserify to parcel

**ver 2.3.2 | 18/11/2018**

1. More friendly prompt text for first use or error display.
Expand Down
2 changes: 1 addition & 1 deletion dest/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CSS Used",
"version": "2.3.3",
"version": "2.4.0",
"permissions": ["<all_urls>","tabs"],
"author": "Bob",
"icons": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "css-used-chrome-ext",
"version": "2.3.3",
"version": "2.4.0",
"description": "A chrome extension to get all css rules used by the selected DOM and its children.",
"main": "./src/main.js",
"scripts": {
Expand Down
37 changes: 21 additions & 16 deletions src/filterRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
// each testing is wrapped by a settimeout timmer to make it async
// because the testing can be a long time if too many.

var debugMode = false;
var debugMode = process.env.NODE_ENV!=='production';
const cssHelper = require('./cssHelper');

// may match accoding to interaction
const pseudocls = '((-(webkit|moz|ms|o)-)?(full-screen|fullscreen))|-o-prefocus|active|checked|disabled|empty|enabled|focus|hover|in-range|invalid|link|out-of-range|target|valid|visited',
pseudoele = '((-(webkit|moz|ms|o)-)?(focus-inner|input-placeholder|placeholder|selection|resizer|scrollbar(-(button|thumb|corner|track(-piece)?))?))|-ms-(clear|reveal|expand)|-moz-(focusring)|-webkit-(details-marker)|after|before|first-letter|first-line';
const PseudoClass = '((-(webkit|moz|ms|o)-)?(full-screen|fullscreen))|-o-prefocus|active|checked|disabled|empty|enabled|focus|hover|in-range|invalid|link|out-of-range|target|valid|visited',
PseudoElement = '((-(webkit|moz|ms|o)-)?(focus-inner|input-placeholder|placeholder|selection|resizer|scrollbar(-(button|thumb|corner|track(-piece)?))?))|-ms-(clear|reveal|expand)|-moz-(focusring)|-webkit-(details-marker)|after|before|first-letter|first-line',
MaxPossiblePseudoLength=30,
REG0=new RegExp('^(:(' + PseudoClass + ')|::?(' + PseudoElement + '))+$', ''),
REG1=new RegExp('( |^)(:(' + PseudoClass + ')|::?(' + PseudoElement + '))+( |$)', 'ig'),
REG2=new RegExp('\\((:(' + PseudoClass + ')|::?(' + PseudoElement + '))+\\)', 'ig'),
REG3=new RegExp('(:(' + PseudoClass + ')|::?(' + PseudoElement + '))+', 'ig');

function filterRules($0, objCss, taskTimerRecord) {
var promises = [];
Expand All @@ -29,7 +34,7 @@ function filterRules($0, objCss, taskTimerRecord) {
objCss.normRule.forEach(function (rule, idx) {
promises.push(new Promise(function (res, rej) {
var timer = setTimeout(function () {
if (idx % 100 === 0) {
if (idx % 1000 === 0) {
chrome.runtime.sendMessage({
dom: domlist.length - 1,
rule: objCss.normRule.length,
Expand All @@ -53,21 +58,21 @@ function filterRules($0, objCss, taskTimerRecord) {
// but wont apply now
// eg. :active{xxx}
// only works when clicked on and actived
if (sel.match(new RegExp('^(:(' + pseudocls + ')|::?(' + pseudoele + '))+$', ''))) {
if (sel.length<MaxPossiblePseudoLength && sel.match(REG0)){
selMatched.push(sel);
} else {
let count = [];
let replacedSel = sel.replace(new RegExp('( |^)(:(' + pseudocls + ')|::?(' + pseudoele + '))+( |$)', 'ig'), ' * ');
replacedSel = replacedSel.replace(new RegExp('\\((:(' + pseudocls + ')|::?(' + pseudoele + '))+\\)', 'ig'), '(*)');
replacedSel = replacedSel.replace(new RegExp('(:(' + pseudocls + ')|::?(' + pseudoele + '))+', 'ig'), '');
try {
if ($0.matches(sel) || $0.querySelectorAll(sel).length !== 0) {
selMatched.push(sel);
}
} catch (e) {
count.push(sel);
count.push(e);
}
let replacedSel = sel.replace(REG1, ' * ')
.replace(REG2, '(*)')
.replace(REG3, '');
// try {
// if ($0.matches(sel) || $0.querySelectorAll(sel).length !== 0) {
// selMatched.push(sel);
// }
// } catch (e) {
// count.push(sel);
// count.push(e);
// }
try {
if ($0.matches(replacedSel) || $0.querySelectorAll(replacedSel).length !== 0) {
selMatched.push(sel);
Expand Down
41 changes: 21 additions & 20 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
let wSocket = new WebSocket('ws://' + 'localhost' + ':' + '12345' + '');

wSocket.onopen = function() {
// console.log("Primary Socket Connected.");
};

wSocket.onmessage = function(evt) {
// console.log(evt);
location.reload();
}

wSocket.onclose = function() {
console.log("Primary Socket Closed.");
wSocket = null;
};

wSocket.onerror = function(evt) {
console.error(evt);
}

import expected from './expected.js'
import getCssUsed from '../src/main.js'

Expand All @@ -18,24 +38,5 @@ window.chrome = {
}
}
}
getCssUsed(document.documentElement);

let wSocket = new WebSocket('ws://' + 'localhost' + ':' + '12345' + '');

wSocket.onopen = function() {
// console.log("Primary Socket Connected.");
};

wSocket.onmessage = function(evt) {
// console.log(evt);
location.reload();
}

wSocket.onclose = function() {
console.log("Primary Socket Closed.");
wSocket = null;
};

wSocket.onerror = function(evt) {
console.error(evt);
}
getCssUsed(document.documentElement);
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const entryFiles = Path.join(__dirname, './index.js');
const options = {
outDir: './test',
outFile: 'index.build.js',
scopeHoist: true, // Turn on experimental scope hoisting/tree shaking flag, for smaller production bundles
// sourceMaps: true,
hmrPort: 12345, // hmr socket port
};

Expand Down

0 comments on commit 675770b

Please sign in to comment.