Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
finish implementing rule page scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
jspenguin2017 committed Dec 21, 2017
1 parent 22df038 commit b07325c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
93 changes: 87 additions & 6 deletions src/js/nano-dashboard-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ window.tabRules = new class tabRules extends Tab {
}

readOneSettings("nanoEditorWordSoftWrap").then((data) => {
console.assert(typeof data === "boolean");
console.assert(this.editor === nanoIDE.editor);

nanoIDE.setLineWrap(data);

vAPI.messaging.send("dashboard", { what: "readUserFilters" }, (data) => {
console.assert(data && typeof data === "object");
console.assert(this.editor === nanoIDE.editor);

if (data.error) {
Expand Down Expand Up @@ -114,10 +117,12 @@ window.tabRules = new class tabRules extends Tab {
return nanoIDE.getLinuxValue();
},
(data) => {
// TODO
// Pull and overwrite
this.onCloudPull(data, false);
},
(data) => {
// TODO
// Pull and merge
this.onCloudPull(data, true);
},
);
}
Expand Down Expand Up @@ -159,6 +164,21 @@ window.tabRules = new class tabRules extends Tab {
}
this.changeCheckerTimer = setTimeout(this.changeChecker, 250);
}
/**
* Cloud pull.
* @method
* @param {string} data - The incoming cloud data.
* @param {boolean} merge - Whether the data should be merged into existing ones.
*/
onCloudPull(data, merge) {
console.assert(typeof data === "string" && typeof merge === "boolean");
console.assert(this.editor === nanoIDE.editor);

if (merge) {
data = uBlockDashboard.mergeNewLines(nanoIDE.getLinuxValue(), data);
}
nanoIDE.setValueFocus(data);
}
/**
* Handle import from file.
* @method
Expand All @@ -169,7 +189,48 @@ window.tabRules = new class tabRules extends Tab {
console.assert(elem instanceof HTMLInputElement && elem.type === "file");
console.assert(typeof append === "boolean");

const file = elem.files[0];
if (!file || !file.name || !file.type.startsWith("text")) {
return;
}

const reader = new FileReader();
reader.onload = () => {
let data = reader.result;
let processed = null;

console.assert(typeof data === "string");

const reAbpSubscriptionExtractor = /\n\[Subscription\]\n+url=~[^\n]+([\x08-\x7E]*?)(?:\[Subscription\]|$)/ig;
const reAbpFilterExtractor = /\[Subscription filters\]([\x08-\x7E]*?)(?:\[Subscription\]|$)/i;

const matches = reAbpSubscriptionExtractor.exec(data);
if (matches === null) {
processed = data;
} else {
let out = [];
let filterMatch;
while (matches !== null) {
if (matches.length === 2) {
filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
if (filterMatch !== null && filterMatch.length === 2) {
out.push(filterMatch[1].trim().replace(/\\\[/g, "["));
}
}
matches = reAbpSubscriptionExtractor.exec(s);
}
processed = out.join("\n");
}

console.assert(this.editor === nanoIDE.editor);

if (append) {
nanoIDE.setValueFocus(nanoIDE.getLinuxValue() + "\n" + processed);
} else {
nanoIDE.setValueFocus(processed);
}
};
reader.readAsText(file);
}

// ===== Button Handlers =====
Expand Down Expand Up @@ -202,6 +263,12 @@ window.tabRules = new class tabRules extends Tab {
}
},
);

this.btnApply.MaterialButton.disable();
this.btnRevert.MaterialButton.disable();

closeAllTooltips();

}
/**
* Revert button click handler.
Expand All @@ -213,16 +280,19 @@ window.tabRules = new class tabRules extends Tab {

nanoIDE.setValueFocus(this.lastSavedData);
this.changed = false;

this.btnApply.MaterialButton.disable();
this.btnRevert.MaterialButton.disable();

closeAllTooltips();
}
/**
* Import and append button click handler.
* @method
*/
onImportAppendBtnClicked() {
console.assert(this.editor === nanoIDE.editor);

pickFile((elem) => {
this.onRestoreFilePicked(elem, true);
this.onImportFilePicked(elem, true);
});
}
/**
Expand All @@ -232,6 +302,17 @@ window.tabRules = new class tabRules extends Tab {
onExportBtnClicked() {
console.assert(this.editor === nanoIDE.editor);

// TODO
const val = this.editor.getValue();
if (val.trim() === "") {
return;
}

const name = vAPI.i18n("1pExportFilename")
.replace("{{datetime}}", uBlockDashboard.dateNowToSensibleString())
.replace(/ +/g, "_");
vAPI.download({
"url": "data:text/plain;charset=utf-8," + encodeURIComponent(val),
"filename": name,
});
}
};
10 changes: 5 additions & 5 deletions src/js/nano-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ Cloud.redrawUI = (callback) => {
},
(data) => {
if (data && typeof data === "object") {
Cloud.data = data;
Cloud.data = data.data;

Cloud.pullBtn.MaterialButton.enabled();
Cloud.mergeBtn.MaterialButton.enabled();
Cloud.pullBtn.MaterialButton.enable();
Cloud.mergeBtn.MaterialButton.enable();

const time = new Date(data.tstamp);
Cloud.contentElem.textContent = vAPI.i18n("nanoCloudLastSync")
Expand Down Expand Up @@ -395,7 +395,7 @@ Cloud.pushBtnClick = () => {
* @private @function
*/
Cloud.pullBtnClicked = () => {
console.assert(Cloud.data && typeof Cloud.data === "object" && typeof Cloud.onPull === "function");
console.assert(Cloud.data && typeof Cloud.data === "string" && typeof Cloud.onPull === "function");

Cloud.onPull(Cloud.data);
};
Expand All @@ -404,7 +404,7 @@ Cloud.pullBtnClicked = () => {
* @private @function
*/
Cloud.mergeBtnClicked = () => {
console.assert(Cloud.data && typeof Cloud.data === "object" && typeof Cloud.onMerge === "function");
console.assert(Cloud.data && typeof Cloud.data === "string" && typeof Cloud.onMerge === "function");

Cloud.onMerge(Cloud.data);
};
Expand Down

0 comments on commit b07325c

Please sign in to comment.