Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update functions.js #2057

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 41 additions & 212 deletions menu/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,234 +2,63 @@
>>> FUNCTIONS:
----------------------------------------------------------------
# Attributes
# Export settings
# Import settings
--------------------------------------------------------------*/

/*--------------------------------------------------------------
# ATTRIBUTES
--------------------------------------------------------------*/

extension.attributes = function () {
var attributes = {
theme: true,
improvedtube_home: true,
title_version: true,
it_general: true,
it_appearance: true,
it_themes: true,
it_player: true,
it_playlist: true,
it_channel: true,
it_shortcuts: true,
it_blocklist: true,
it_analyzer: true,
layer_animation_scale: false
};

for (var attribute in attributes) {
var value = satus.storage.get(attribute);

if (attribute === 'improvedtube_home') {
attribute = 'home-style';
}

if (satus.isset(value)) {
extension.skeleton.rendered.setAttribute(attribute.replace('it_', '').replace(/_/g, '-'), value);
}
}
};


/*--------------------------------------------------------------
# EXPORT SETTINGS
--------------------------------------------------------------*/

extension.exportSettings = function () {
if (location.href.indexOf('action=export-settings') !== -1) {
satus.render({
component: 'modal',
variant: 'confirm',
content: 'areYouSureYouWantToExportTheData',
buttons: {
cancel: {
component: 'button',
text: 'cancel',
on: {
click: function () {
this.modalProvider.close();
}
}
},
ok: {
component: 'button',
text: 'ok',
on: {
click: function () {
try {
var blob = new Blob([JSON.stringify(satus.storage.data)], {
type: 'application/json;charset=utf-8'
});

chrome.permissions.request({
permissions: ['downloads']
}, function (granted) {
if (granted) {
chrome.downloads.download({
url: URL.createObjectURL(blob),
filename: 'improvedtube.json',
saveAs: true
}, function () {
setTimeout(function () {
close();
}, 1000);
});
}
});
} catch (error) {
console.error(error);
}
}
}
}
}
}, extension.skeleton.rendered);
}
var attributes = {
theme: true,
improvedtube_home: true,
title_version: true,
it_general: true,
it_appearance: true,
it_themes: true,
it_player: true,
it_playlist: true,
it_channel: true,
it_shortcuts: true,
it_blocklist: true,
it_analyzer: true,
layer_animation_scale: false
};

for (var attribute in attributes) {
var value = satus.storage.get(attribute);

if (attribute === 'improvedtube_home') {
attribute = 'home-style';
}

if (satus.isset(value)) {
extension.skeleton.rendered.setAttribute(attribute.replace('it_', '').replace(/_/g, '-'), value);
}
}
};

// The remaining functions related to exporting, importing, and syncing settings are removed as they are replaced by the following code.

/*--------------------------------------------------------------
# IMPORT SETTINGS
# SYNC SETTINGS
--------------------------------------------------------------*/

extension.importSettings = function () {
if (location.href.indexOf('action=import-settings') !== -1) {
satus.render({
component: 'modal',
variant: 'confirm',
content: 'areYouSureYouWantToImportTheData',
buttons: {
cancel: {
component: 'button',
text: 'cancel',
on: {
click: function () {
this.modalProvider.close();
}
}
},
ok: {
component: 'button',
text: 'ok',
on: {
click: function () {
var input = document.createElement('input');

input.type = 'file';

input.addEventListener('change', function () {
var file_reader = new FileReader();

file_reader.onload = function () {
var data = JSON.parse(this.result);
for (var key in data) {
satus.storage.set(key, data[key]);
}

setTimeout(function () {
chrome.runtime.sendMessage({
action: 'import-settings'
});

setTimeout(function () {
close();
}, 128);
}, 256);
};

file_reader.readAsText(this.files[0]);
});

input.click();
}
}
}
}
}, extension.skeleton.rendered);
}
};

extension.syncSettings = function () {
chrome.storage.sync.set(satus.storage.data, function() {
console.log("Settings synchronized.");
});
}

/*--------------------------------------------------------------
# sync SETTINGS
# PULL SETTINGS
--------------------------------------------------------------*/

extension.pushSettings = function () {
satus.render({
component: 'modal',
variant: 'confirm',
content: 'areYouSureYouWantToSyncTheData',
buttons: {
cancel: {
component: 'button',
text: 'cancel',
on: {
click: function () {
this.modalProvider.close();
}
}
},
ok: {
component: 'button',
text: 'ok',
on: {
click: function () {
try {
chrome.storage.sync.clear();
var blob = JSON.stringify(satus.storage.data);
chrome.storage.sync.set({settings: blob});
} catch (error) {
console.error(error);
}
this.modalProvider.close();
}
}
}
}
}, extension.skeleton.rendered);
}

extension.pullSettings = function () {

satus.render({
component: 'modal',
variant: 'confirm',
content: 'areYouSureYouWantToImportTheData',
buttons: {
cancel: {
component: 'button',
text: 'cancel',
on: {
click: function () {
this.modalProvider.close();
}
}
},
ok: {
component: 'button',
text: 'ok',
on: {
click: function () {
chrome.storage.sync.get('settings', function (r) {
var data = JSON.parse(r['settings']);
for (var key in data) {
satus.storage.set(key, data[key]);
}
});
this.modalProvider.close();
}
}
}
}
}, extension.skeleton.rendered);
chrome.storage.sync.get(null, function(syncedData) {
for (var key in syncedData) {
satus.storage.set(key, syncedData[key]);
}
console.log("Settings pulled from synced storage.");
});
}