Skip to content

Commit 8b05871

Browse files
authored
🔀 Merge pull request #569 from FrostCo/bookmarklet_fixes
Bookmarklet Fixes
2 parents e3c6d66 + edbfa78 commit 8b05871

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

src/script/bookmarkletFilter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default class BookmarkletFilter extends Filter {
1919
processNode: (node: Document | HTMLElement | Node | ShadowRoot, wordlistId: number, statsType?: string | null) => void;
2020
shadowObserver: MutationObserver;
2121
stats?: Statistics; // Bookmarklet: Not used
22-
updateCounterBadge: () => void; // Bookmarklet: Not used - Needed to match signature of WebFilter
2322

2423
//#region Class reference helpers
2524
// Can be overridden in children classes
@@ -104,6 +103,7 @@ export default class BookmarkletFilter extends Filter {
104103

105104
cleanPage(config?: WebConfig) {
106105
this.cfg = new this.Class.Config(config);
106+
this.cfg.collectStats = false; // Bookmarklet: Force disable collection of stats
107107
this.filterText = this.cfg.filterMethod !== this.Class.Constants.FILTER_METHODS.OFF;
108108
this.domain = this.Class.Domain.byHostname(this.hostname, this.cfg.domains);
109109

@@ -286,4 +286,8 @@ export default class BookmarkletFilter extends Filter {
286286
observer.disconnect();
287287
if (mutations) { this.processMutations(mutations); }
288288
}
289+
290+
updateCounterBadge() {
291+
// Bookmarklet: Not used - Needed to match signature of WebFilter
292+
}
289293
}

src/script/lib/helper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export function booleanToNumber(value: boolean): number {
44
return value ? Constants.TRUE : Constants.FALSE;
55
}
66

7+
export function deepCloneJson(object) {
8+
return JSON.parse(JSON.stringify(object));
9+
}
10+
711
/* istanbul ignore next */
812
export function dynamicList(list: string[], select: HTMLSelectElement, upperCaseFirstChar: boolean = false, title?: string) {
913
removeChildren(select);

src/script/optionPage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Bookmarklet from '@APF/bookmarklet';
88
import Logger from '@APF/lib/logger';
99
import {
1010
booleanToNumber,
11+
deepCloneJson,
1112
dynamicList,
1213
exportToFile,
1314
lastElement,
@@ -262,6 +263,15 @@ export default class OptionPage {
262263
configText.value = JSON.stringify(config, null, 2);
263264
}
264265

266+
get bookmarkletConfig() {
267+
const clone = deepCloneJson(this.cfg);
268+
const keysToRemove = [...this.cfg.Class._localOnlyKeys];
269+
Object.keys(clone).forEach((key) => {
270+
if (keysToRemove.includes(key)) delete clone[key];
271+
});
272+
return clone;
273+
}
274+
265275
bulkEditorAddRow(word: string = '', data: WordOptions | undefined = undefined) {
266276
const table = document.querySelector('#bulkWordEditorModal table#bulkWordEditorTable') as HTMLTableElement;
267277
if (data === undefined) {
@@ -864,7 +874,7 @@ export default class OptionPage {
864874

865875
const bookmarkletConfig = document.querySelector('input[name="bookmarkletConfig"]:checked') as HTMLInputElement;
866876
const bookmarkletLink = document.getElementById('bookmarkletLink') as HTMLAnchorElement;
867-
const cfg = bookmarkletConfig.value == 'default' ? null : this.cfg;
877+
const cfg = bookmarkletConfig.value == 'default' ? null : this.bookmarkletConfig;
868878
const href = this.bookmarklet.href(cfg);
869879
bookmarkletLink.href = href;
870880
this.Class.enableBtn(bookmarkletLink);

test/spec/lib/helper.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from 'chai';
22
import Constants from '@APF/lib/constants';
33
import {
44
booleanToNumber,
5+
deepCloneJson,
56
formatNumber,
67
getParent,
78
getVersion,
@@ -33,6 +34,50 @@ describe('Helper', function() {
3334
});
3435
});
3536

37+
describe('deepJsonClone()', function() {
38+
const deepObject = {
39+
children: {
40+
deep: false,
41+
name: 'shallow',
42+
shallow: true,
43+
},
44+
deep: true,
45+
name: 'deep',
46+
numbers: [1, 2],
47+
shallow: false,
48+
strings: ['one', 'two'],
49+
};
50+
51+
const shallowObject = {
52+
deep: false,
53+
name: 'shallow',
54+
shallow: true,
55+
};
56+
57+
it('Shallow clones object', function() {
58+
expect(JSON.stringify(deepCloneJson(shallowObject))).to.eql(JSON.stringify(shallowObject));
59+
});
60+
61+
it('Deep clones object', function() {
62+
expect(JSON.stringify(deepCloneJson(deepObject))).to.eql(JSON.stringify(deepObject));
63+
});
64+
65+
it('Is a clone (delete)', function() {
66+
const clone = deepCloneJson(deepObject);
67+
delete clone.name;
68+
expect(clone.name).to.be.undefined;
69+
expect(deepObject.name).to.not.be.undefined;
70+
});
71+
72+
it('Is a clone (update)', function() {
73+
const clone = deepCloneJson(deepObject);
74+
const key = 'name2';
75+
clone[key] = 'deep2';
76+
expect(clone[key]).to.not.be.undefined;
77+
expect(deepObject[key]).to.be.undefined;
78+
});
79+
});
80+
3681
describe('formatNumber()', function() {
3782
it('Format numbers for counter display', function() {
3883
expect(formatNumber(999)).to.eql('999');

0 commit comments

Comments
 (0)