Skip to content

Commit

Permalink
Merge pull request #280 from hawkeye116477/master
Browse files Browse the repository at this point in the history
Add a link to the remote asset in asset viewer
  • Loading branch information
JustOff authored Dec 1, 2020
2 parents 52eb93b + 0608430 commit 5ecbd31
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/1p-filters.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<link rel="stylesheet" type="text/css" href="css/themes/default.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/fa-icons.css">
<link rel="stylesheet" href="css/dashboard-common.css">
<link rel="stylesheet" href="css/cloud-ui.css">
<link rel="stylesheet" href="css/1p-filters.css">
Expand Down Expand Up @@ -45,6 +46,7 @@
<script src="js/codemirror/search.js"></script>
<script src="js/codemirror/ubo-static-filtering.js"></script>

<script src="js/fa-icons.js"></script>
<script src="js/vapi.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-client.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions src/asset-viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<link rel="stylesheet" type="text/css" href="css/themes/default.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/fa-icons.css">
<link rel="stylesheet" href="css/codemirror.css">
<style>
body {
Expand Down Expand Up @@ -36,6 +37,7 @@
<script src="js/codemirror/search.js"></script>
<script src="js/codemirror/ubo-static-filtering.js"></script>

<script src="js/fa-icons.js"></script>
<script src="js/vapi.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-client.js"></script>
Expand Down
17 changes: 15 additions & 2 deletions src/css/codemirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@
direction: ltr;
display: flex;
flex-shrink: 0;
font-size: 110%;
justify-content: center;
padding: 0.5em;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
z-index: 1000;
}
.cm-search-widget .fa {
color: #888;
.cm-search-widget .fa-icon {
fill: #888;
font-size: 140%;
}
.cm-search-widget .fa-icon:not(.fa-icon-ro):hover {
fill: #000;
}
.cm-search-widget-input {
border: 1px solid gray;
border-radius: 3px;
Expand Down Expand Up @@ -74,6 +78,15 @@
.cm-search-widget .cm-search-widget-button:hover {
color: #000;
}
.cm-search-widget .sourceURL {
padding-left: 0.5em;
padding-right: 0.5em;
position: absolute;
left: 0;
}
.cm-search-widget .sourceURL[href=""] {
display: none;
}

.CodeMirror-merge-l-deleted {
background-image: none;
Expand Down
7 changes: 6 additions & 1 deletion src/js/asset-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
},
function(details) {
cmEditor.setValue(details && details.content || '');
}
if ( details.sourceURL ) {
const a = document.querySelector('.cm-search-widget .sourceURL');
a.setAttribute('href', details.sourceURL);
a.setAttribute('title', details.sourceURL);
}
}
);

var cmEditor = new CodeMirror(
Expand Down
45 changes: 28 additions & 17 deletions src/js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,17 +854,29 @@ api.get = function(assetKey, options, callback) {
contentURLs,
contentURL;

var reportBack = function(content, err) {
var details = { assetKey: assetKey, content: content };
let reportBack = (content, err) => {
const details = { assetKey, content };
if ( err ) {
details.error = assetDetails.lastError = err;
} else {
assetDetails.lastError = undefined;
}
if ( options.needSourceURL ) {
if (
contentURL === undefined &&
assetCacheRegistry instanceof Object &&
assetCacheRegistry[assetKey] instanceof Object
) {
details.sourceURL = assetCacheRegistry[assetKey].remoteURL;
}
if ( reIsExternalPath.test(contentURL) ) {
details.sourceURL = contentURL;
}
}
callback(details);
};

var onContentNotLoaded = function() {
let onContentNotLoaded = ( ) => {
var isExternal;
while ( (contentURL = contentURLs.shift()) ) {
isExternal = reIsExternalPath.test(contentURL);
Expand All @@ -883,7 +895,7 @@ api.get = function(assetKey, options, callback) {
}
};

var onContentLoaded = function(details) {
let onContentLoaded = details => {
if ( stringIsNotEmpty(details.content) === false ) {
onContentNotLoaded();
return;
Expand All @@ -897,7 +909,7 @@ api.get = function(assetKey, options, callback) {
reportBack(details.content);
};

var onCachedContentLoaded = function(details) {
let onCachedContentLoaded = details => {
if ( details.content !== '' ) {
return reportBack(details.content);
}
Expand Down Expand Up @@ -1003,36 +1015,35 @@ api.put = function(assetKey, content, callback) {
/******************************************************************************/

api.metadata = function(callback) {
var assetRegistryReady = false,
let assetRegistryReady = false,
cacheRegistryReady = false;

var onReady = function() {
var assetDict = JSON.parse(JSON.stringify(assetSourceRegistry)),
cacheDict = assetCacheRegistry,
assetEntry, cacheEntry,
now = Date.now(), obsoleteAfter;
for ( var assetKey in assetDict ) {
assetEntry = assetDict[assetKey];
cacheEntry = cacheDict[assetKey];
let onReady = function() {
let assetDict = JSON.parse(JSON.stringify(assetSourceRegistry));
const cacheDict = assetCacheRegistry;
const now = Date.now();
for ( let assetKey in assetDict ) {
const assetEntry = assetDict[assetKey];
const cacheEntry = cacheDict[assetKey];
if ( cacheEntry ) {
assetEntry.cached = true;
assetEntry.writeTime = cacheEntry.writeTime;
obsoleteAfter = cacheEntry.writeTime + assetEntry.updateAfter * 86400000;
const obsoleteAfter =
cacheEntry.writeTime + assetEntry.updateAfter * 86400000;
assetEntry.obsolete = obsoleteAfter < now;
assetEntry.remoteURL = cacheEntry.remoteURL;
} else if (
assetEntry.contentURL &&
assetEntry.contentURL.length !== 0
) {
assetEntry.writeTime = 0;
obsoleteAfter = 0;
assetEntry.obsolete = true;
}
}
callback(assetDict);
};

getAssetSourceRegistry(function() {
getAssetSourceRegistry(( ) => {
assetRegistryReady = true;
if ( cacheRegistryReady ) { onReady(); }
});
Expand Down
44 changes: 26 additions & 18 deletions src/js/codemirror/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@
};
}

var searchWidgetHtml =
'<div class="cm-search-widget">' +
'<span class="fa">&#xf002;</span>&ensp;' +
'<span class="cm-search-widget-input">' +
'<input type="text">' +
'<span class="cm-search-widget-count">' +
'<span><!-- future use --></span><span>0</span>' +
'</span>' +
'</span>&ensp;' +
'<span class="cm-search-widget-up cm-search-widget-button fa">&#xf077;</span>&ensp;' +
'<span class="cm-search-widget-down cm-search-widget-button fa">&#xf078;</span>&ensp;' +
'</div>';

function searchWidgetKeydownHandler(cm, ev) {
var keyName = CodeMirror.keyName(ev);
if ( !keyName ) { return; }
Expand Down Expand Up @@ -129,10 +116,9 @@
this.query = null;
this.overlay = null;
this.panel = null;
this.widget = null;
var domParser = new DOMParser();
var doc = domParser.parseFromString(searchWidgetHtml, 'text/html');
this.widget = document.adoptNode(doc.body.firstElementChild);
const widgetParent =
document.querySelector('.cm-search-widget-template').cloneNode(true);
this.widget = widgetParent.children[0];
this.widget.addEventListener('keydown', searchWidgetKeydownHandler.bind(null, cm));
this.widget.addEventListener('input', searchWidgetInputHandler.bind(null, cm));
this.widget.addEventListener('mousedown', searchWidgetClickHandler.bind(null, cm));
Expand All @@ -146,7 +132,7 @@
// We want the search widget to behave as if the focus was on the
// CodeMirror editor.

var reSearchCommands = /^(?:find|findNext|findPrev|newlineAndIndent)$/;
const reSearchCommands = /^(?:find|findNext|findPrev|newlineAndIndent)$/;

function widgetCommandHandler(cm, command) {
if ( reSearchCommands.test(command) === false ) { return false; }
Expand Down Expand Up @@ -316,6 +302,28 @@
if ( state.query ) { return findNext(cm, true); }
}

{
const searchWidgetTemplate =
'<div class="cm-search-widget-template" style="display:none;">' +
'<div class="cm-search-widget">' +
'<span class="fa-icon fa-icon-ro">search</span>&ensp;' +
'<span class="cm-search-widget-input">' +
'<input type="text">' +
'<span class="cm-search-widget-count">' +
'<span><!-- future use --></span><span>0</span>' +
'</span>' +
'</span>&ensp;' +
'<span class="cm-search-widget-up cm-search-widget-button fa-icon">angle-up</span>&ensp;' +
'<span class="cm-search-widget-down cm-search-widget-button fa-icon fa-icon-vflipped">angle-up</span>&ensp;' +
'<a class="fa-icon sourceURL" href>external-link</a>' +
'</div>' +
'</div>';
const domParser = new DOMParser();
const doc = domParser.parseFromString(searchWidgetTemplate, 'text/html');
const widgetTemplate = document.adoptNode(doc.body.firstElementChild);
document.body.appendChild(widgetTemplate);
}

CodeMirror.commands.find = findCommand;
CodeMirror.commands.findNext = findNextCommand;
CodeMirror.commands.findPrev = findPrevCommand;
Expand Down
6 changes: 5 additions & 1 deletion src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ const onMessage = function(request, sender, callback) {
switch ( request.what ) {
case 'getAssetContent':
// https://github.com/chrisaljoudi/uBlock/issues/417
µb.assets.get(request.url, { dontCache: true }, callback);
µb.assets.get(
request.url,
{ dontCache: true, needSourceURL: true },
callback
);
return;

case 'listsFromNetFilter':
Expand Down
2 changes: 2 additions & 0 deletions src/whitelist.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<link rel="stylesheet" type="text/css" href="css/themes/default.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/fa-icons.css">
<link rel="stylesheet" href="css/dashboard-common.css">
<link rel="stylesheet" href="css/cloud-ui.css">
<link rel="stylesheet" href="css/whitelist.css">
Expand Down Expand Up @@ -47,6 +48,7 @@

<script src="js/codemirror/search.js"></script>

<script src="js/fa-icons.js"></script>
<script src="js/vapi.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-client.js"></script>
Expand Down

0 comments on commit 5ecbd31

Please sign in to comment.