"inline" checkbox opens search results within popup.

Unchecked => opens new tab for search page.

Checkbox hidden if inline results are shown (still shows link to "find styles" which opens a new tab by-default).
This commit is contained in:
derv82 2017-12-03 20:24:39 -08:00
parent 8d75042f02
commit b3cd06bedd
5 changed files with 59 additions and 64 deletions

View File

@ -369,22 +369,22 @@
"message": "Install", "message": "Install",
"description": "Text for a button that installs a search result" "description": "Text for a button that installs a search result"
}, },
"searchResultInstallTooltip": {
"message": "Install this search result",
"description": "Tooltip for a button that installs a search result"
},
"searchResultCustomize": { "searchResultCustomize": {
"message": "Customize", "message": "Customize",
"description": "Text for a button that customizes a search result before installing" "description": "Text for a button that customizes a search result before installing"
}, },
"searchResultCustomizeTooltip": { "searchResultCustomizeTooltip": {
"message": "Customize this style on userstyles.org", "message": "Open this style on userstyles.org to customize via 'Advanced Style Settings'",
"description": "Tooltip for a button that customizes a search result before installing" "description": "Tooltip for a button that opens style on userstyles.org for customizing"
}, },
"findStylesForSite": { "findStylesForSite": {
"message": "Find more styles for this site", "message": "Find more styles for this site",
"description": "Text for a link that gets a list of styles for the current site" "description": "Text for a link that gets a list of styles for the current site"
}, },
"findStylesForSiteInline": {
"message": "Inline",
"description": "Text for a checkbox that opens search results 'inline' (within the Stylus popup window)"
},
"helpAlt": { "helpAlt": {
"message": "Help", "message": "Help",
"description": "Alternate text for help buttons" "description": "Alternate text for help buttons"

View File

@ -89,7 +89,7 @@
<a class="searchResult-title" target="_blank"></a> <a class="searchResult-title" target="_blank"></a>
<div class="searchResult-description"></div> <div class="searchResult-description"></div>
<div class="searchResult-author"> <div class="searchResult-author">
By: <label i18n-text="author"></label>
<a class="searchResult-authorLink" target="_blank"></a> <a class="searchResult-authorLink" target="_blank"></a>
</div> </div>
<div class="searchResult-meta"> <div class="searchResult-meta">
@ -103,8 +103,10 @@
</div> </div>
</div> </div>
<div class="actions"> <div class="actions">
<button class="searchResult-install" i18n-text="searchResultInstall" i18n-title="searchResultInstallTooltip"></button> <button class="searchResult-install" i18n-text="installButton"></button>
<button class="searchResult-customize hidden" i18n-text="searchResultCustomize" i18n-title="searchResultCustomizeTooltip"></button> <button class="searchResult-customize hidden"
i18n-text="searchResultCustomize"
i18n-title="searchResultCustomizeTooltip"></button>
</div> </div>
</div> </div>
</template> </template>
@ -165,14 +167,13 @@
<div class="left-gutter"></div> <div class="left-gutter"></div>
<div class="main-controls"> <div class="main-controls">
<div id="find-styles"> <div id="find-styles">
<a id="find-styles-link" href="#" <a id="find-styles-link" i18n-text="findStylesForSite"></a>
i18n-text="findStylesForSite"></a> <span id="find-styles-inline-group">
<input id="find-styles-inline" class="checker" type="checkbox" />
<label for="find-styles-inline" i18n-text="findStylesForSiteInline"></label>
</span>
</div> </div>
<div id="searchResults-error" class="hidden"></div> <div id="searchResults-error" class="hidden"></div>
<div id="open-search" class="hidden">
<a id="open-search-link" href="https://userstyles.org/styles/browse/"
i18n-text="openSearchWebsite"></a>
</div>
<div id="write-style"> <div id="write-style">
<span id="write-style-for" i18n-text="writeStyleFor"></span> <span id="write-style-for" i18n-text="writeStyleFor"></span>
</div> </div>

View File

@ -111,14 +111,6 @@ function initPopup(url) {
return; return;
} }
const u = tryCatch(() => new URL(url));
$('#open-search-link').onclick = handleEvent.openURLandHide;
$('#open-search-link').href +=
!u ? '' :
u.protocol === 'file:' ? 'file:' :
u.protocol === location.protocol ? '?search_terms=Stylus' :
u.hostname.replace(/^www\.|(\.com?)?\.\w+$/g, '').split('.').pop();
getActiveTab().then(function ping(tab, retryCountdown = 10) { getActiveTab().then(function ping(tab, retryCountdown = 10) {
sendMessage({tabId: tab.id, method: 'ping', frameId: 0}, pong => { sendMessage({tabId: tab.id, method: 'ping', frameId: 0}, pong => {
if (pong) { if (pong) {

View File

@ -8,12 +8,8 @@
display: block; display: block;
} }
#searchResults.hidden, .hidden {
#searchResults-error.hidden, display: none !important;
#find-styles.hidden,
#open-search.hidden,
.searchResult-customize.hidden {
display: none;
} }
#searchResults-error { #searchResults-error {

View File

@ -2,12 +2,25 @@
'use strict'; 'use strict';
(() => { (() => {
onDOMready().then(() => { Promise.all([getActiveTab(), onDOMready()])
const searchResults = searchResultsController(); .then(([tab]) => {
$('#find-styles-link').onclick = searchResults.load; $('#find-styles-link').href = searchUserstyles().getSearchPageURL(tab.url);
$('#searchResultsNav-prev').onclick = searchResults.prev;
$('#searchResultsNav-next').onclick = searchResults.next; $('#find-styles-link').onclick = event => {
}); // Only load search results inline if option is selected.
if ($('#find-styles-inline').checked) {
$('#find-styles-inline-group').classList.add('hidden');
$('#find-styles-inline').checked = false;
const searchResults = searchResultsController();
$('#searchResultsNav-prev').onclick = searchResults.prev;
$('#searchResultsNav-next').onclick = searchResults.next;
// Intercept event to avoid opening the search page.
return searchResults.load(event);
} else {
handleEvent.openURLandHide.call($('#find-styles-link'), event);
}
};
});
/** /**
* Represents the search results within the Stylus popup. * Represents the search results within the Stylus popup.
@ -144,8 +157,6 @@
return true; return true;
} }
$('#find-styles').classList.add('hidden');
$('#open-search').classList.remove('hidden');
$('#searchResults').classList.remove('hidden'); $('#searchResults').classList.remove('hidden');
$('#searchResults-error').classList.add('hidden'); $('#searchResults-error').classList.add('hidden');
@ -398,7 +409,7 @@ function searchUserstyles() {
let currentPage = 1; let currentPage = 1;
let exhausted = false; let exhausted = false;
return {BASE_URL, getCategory, isExhausted, search, fetchStyleJson, fetchStyle}; return {BASE_URL, getCategory, getSearchPageURL, isExhausted, search, fetchStyleJson, fetchStyle};
/** /**
* @returns {Boolean} If there are no more results to fetch from userstyles.org * @returns {Boolean} If there are no more results to fetch from userstyles.org
@ -407,38 +418,33 @@ function searchUserstyles() {
return exhausted; return exhausted;
} }
function getSearchPageURL(url) {
const category = getCategory(url);
if (category === 'STYLUS') {
return BASE_URL + '/styles/browse/?search_terms=Stylus';
} else {
return BASE_URL + '/styles/browse/' + category;
}
}
/** /**
* Resolves the Userstyles.org "category" for a given URL. * Resolves the Userstyles.org "category" for a given URL.
* @param {String} url The URL to a webpage. * @param {String} url The URL to a webpage.
* @returns {Promise<String>} The category for a URL, or the hostname if category is not found. * @returns {Promise<String>} The category for a URL, or the hostname if category is not found.
*/ */
function getCategory(url) { function getCategory(url) {
let hostname = new URL(url).hostname; const u = tryCatch(() => new URL(url));
// Strip "TLD" (including .com.TLD, .co.TLD, etc) if (!u) {
hostname = hostname.replace(/(\.com|\.co|\.org|\.net|\.gov)?\.[a-z0-9]+$/, ''); return ''; // Invalid URL
// Strip "subdomains" } else if (u.protocol === 'file:') {
hostname = hostname.split(/\./g).pop(); return 'file:'; // File page
return hostname; } else if (u.protocol === location.protocol) {
/* return 'STYLUS'; // Stylus page
// Resolve "category" using userstyles.org's /styles/browse/all/ endpoint: } else {
return new Promise(resolve => { // Website address, strip TLD & subdomain
const request = new XMLHttpRequest(); const domainRegex = /^www\.|(\.com?)?\.\w+$/g;
const browseURL = BASE_URL + '/styles/browse/all/' + encodeURIComponent(url); return u.hostname.replace(domainRegex, '').split('.').pop();
request.open('HEAD', browseURL, true); }
request.onreadystatechange = () => {
if (request.readyState === XMLHttpRequest.DONE) {
const responseURL = new URL(request.responseURL);
const searchCategory = responseURL.searchParams.get('category');
if (searchCategory !== null) {
resolve(searchCategory);
} else {
resolve(hostname);
}
}
};
request.send(null);
});
*/
} }
/** /**