More comments before attempting to install the style

This commit is contained in:
derv82 2017-11-19 14:03:54 -08:00
parent 3503195711
commit 1da3027f8b
2 changed files with 20 additions and 6 deletions

View File

@ -101,6 +101,7 @@
<script src="js/prefs.js"></script> <script src="js/prefs.js"></script>
<script src="content/apply.js"></script> <script src="content/apply.js"></script>
<script src="popup/popup.js"></script> <script src="popup/popup.js"></script>
<script src="js/script-loader.js"></script>
<script src="popup/searchResults.js"></script> <script src="popup/searchResults.js"></script>
</head> </head>

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
let currentPage = 1; let currentPage = 1;
let hostname;
/** /**
* Fetches JSON object from userstyles.org API * Fetches JSON object from userstyles.org API
@ -30,7 +29,7 @@ function fetchUserstylesAPI(path, queryParams) {
reject("Failed to parse JSON from " + url + "\nJSON Text: " + xhr.responseText); reject("Failed to parse JSON from " + url + "\nJSON Text: " + xhr.responseText);
} }
} else { } else {
reject(xhr.status); reject("Error code " + xhr.status);
} }
}; };
xhr.onerror = reject; xhr.onerror = reject;
@ -108,9 +107,21 @@ function createSearchResultElement(searchResult) {
event.preventDefault(); event.preventDefault();
// TODO: Install style // TODO: Install style
fetchUserstylesAPI("/api/v1/styles/" + searchResult.id) fetchUserstylesAPI("/api/v1/styles/" + searchResult.id)
.then( styleObject => { .then(styleObject => {
console.log("TODO: Install style ID", searchResult.id); console.log("TODO: Install style ID", searchResult.id);
console.log("Full styleObject:", styleObject); console.log("Full styleObject:", styleObject);
/*
* Sample full styleObject: https://userstyles.org/api/v1/styles/70271
* The "id" is the ID of the userstyles.org style (e.g. 70271 above)
* saveStyleSafe({...}) expects an "id" referring to the Stylus ID (1-n)
*/
delete styleObject.id;
Object.assign(styleObject, {
enabled: true,
reason: 'update',
notify: true
});
saveStyleSafe(styleObject);
alert("TODO: Install style ID #" + searchResult.id + " name '" + searchResult.name + "'"); alert("TODO: Install style ID #" + searchResult.id + " name '" + searchResult.name + "'");
}) })
.catch(reason => { .catch(reason => {
@ -176,7 +187,7 @@ function loadSearchResults(event) {
$('#searchResults-list').innerHTML = ""; $('#searchResults-list').innerHTML = "";
// Find styles for the current active tab // Find styles for the current active tab
getActiveTab().then(tab => { getActiveTab().then(tab => {
hostname = new URL(tab.url).hostname; const hostname = new URL(tab.url).hostname.replace(/^(?:.*\.)?([^.]*\.(co\.)?[^.]*)$/i, "$1");
const queryParams = [ const queryParams = [
'search=' + encodeURIComponent(hostname), 'search=' + encodeURIComponent(hostname),
'page=' + currentPage, 'page=' + currentPage,
@ -195,10 +206,12 @@ function loadSearchResults(event) {
processSearchResults(code); processSearchResults(code);
}) })
.catch(reason => { .catch(reason => {
throw reason; $('#load-search-results').classList.remove("hidden");
$('#searchResults').classList.add("hidden");
alert("Error while loading search results: " + reason);
}); });
}); });
return false; return true;
} }
onDOMready().then(() => { onDOMready().then(() => {