Skip to content

Commit

Permalink
Replace custom "Open With" with default "Open With"
Browse files Browse the repository at this point in the history
Remove the registry of external applications, and change the option to
use Firefox's default "Open With" dialog (if any).
  • Loading branch information
Rob--W committed Nov 13, 2017
1 parent 8f71646 commit 3c1c1df
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 389 deletions.
6 changes: 3 additions & 3 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* (c) 2013 Rob Wu <[email protected]> (https://robwu.nl)
*/
/* globals Prefs, MimeActions, mime_fromFilename, ModalDialog, OpenWith, ContentHandlers */
/* globals Prefs, MimeActions, mime_fromFilename, ModalDialog, ContentHandlers */
'use strict';

var dialogURL = chrome.extension.getURL('dialog.html');
Expand Down Expand Up @@ -73,7 +73,6 @@ chrome.webRequest.onHeadersReceived.addListener(async function(details) {
guessedMimeType: guessedMimeType,
mimeType: mimeType,
isSniffingMimeType: isSniffingMimeType,
openWithOptions: OpenWith.getAvailableViewers([mimeType, guessedMimeType])
};
var dialog = new ModalDialog({
url: dialogURL + '#' + encodeURIComponent(JSON.stringify(dialogArguments)),
Expand All @@ -98,7 +97,8 @@ chrome.webRequest.onHeadersReceived.addListener(async function(details) {
Prefs.setMimeAction(guessedMimeType, isSniffingMimeType, desiredAction);
}
if (desiredAction.action === MimeActions.OPENWITH) {
return OpenWith.openWith(desiredAction.openWith, details);
// Don't modify the response headers and let the browser handle the request.
return;
}
return {
responseHeaders: details.responseHeaders
Expand Down
4 changes: 1 addition & 3 deletions extension/dialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
<legend data-i18n="actionQuestion">What should brandShortName do with this file?</legend>
<div class="row">
<label id="open-with-container">
<input name="choice" type="radio" value="openwith"> <span>Open with</span>
<select id="open-with">
</select>
<input name="choice" type="radio" value="openwith"> <span data-i18n="openWith">Open with</span> <span data-i18n="brandShortName"></span> <sup>(<span data-i18n="chooseOtherApp">Choose other Application</span>)</sup>
</label>
<label>
<input name="choice" type="radio" value="openas" checked autofocus> <span>Open in browser as</span>
Expand Down
34 changes: 2 additions & 32 deletions extension/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var $ = document.getElementById.bind(document);

var dialogArguments = JSON.parse(decodeURIComponent(window.location.hash.slice(1)));
handleDetails(dialogArguments.url, dialogArguments.filename, dialogArguments.guessedMimeType,
dialogArguments.mimeType, dialogArguments.openWithOptions);
dialogArguments.mimeType);

function handleDetails(url, filename, guessedMimeType, mimeType, openWithOptions) {
function handleDetails(url, filename, guessedMimeType, mimeType) {
// Note: There is so much junk before the title that it is often unreadable,
// at least until https://bugzil.la/1296365 is fixed.
document.title = chrome.i18n.getMessage('opening_title', filename);
Expand All @@ -23,8 +23,6 @@ function handleDetails(url, filename, guessedMimeType, mimeType, openWithOptions

renderURL(url);

renderOpenWithOptions(openWithOptions);

bindFormEvents();

bindPreferenceEvents();
Expand Down Expand Up @@ -175,28 +173,6 @@ function renderURL(/*string*/ url) {
$('url-remainder').textContent = a.pathname + a.search + a.hash;
}

function renderOpenWithOptions(openWithOptions) {
var openWithOptionsLength = openWithOptions.length;
if (openWithOptionsLength === 0) {
$('open-with-container').hidden = true;
return;
}
var options = document.createDocumentFragment();
for (var i = 0; i < openWithOptionsLength; ++i) {
options.appendChild(new Option(openWithOptions[i].label, openWithOptions[i].identifier));
}
var openWithDropdown = $('open-with');
openWithDropdown.appendChild(options);

if (openWithOptionsLength === 1) {
// Just one option. Hide the dropdown and show the viewer's name instead.
openWithDropdown.selectedIndex = 0;
openWithDropdown.hidden = true;
var span = document.createElement('span');
span.textContent = openWithOptions[0].label;
openWithDropdown.parentNode.insertBefore(span, openWithDropdown);
}
}
function bindFormEvents() {
var populateDatalist = function() {
populateDatalist = null;
Expand Down Expand Up @@ -275,7 +251,6 @@ function exportReturnValue() {
case 'openwith':
window.returnValue = {
action: MimeActions.OPENWITH,
openWith: $('open-with').value,
rememberChoice: rememberChoice
};
break;
Expand Down Expand Up @@ -323,11 +298,6 @@ function importReturnValue() {
break;
case MimeActions.OPENWITH:
choice = 'openwith';
$('open-with').value = returnValue.openWith;
if ($('open-with').selectedIndex === -1) {
console.warn('Unknown app "' + returnValue.openWith + '". Was it removed?');
return false;
}
break;
case MimeActions.DOWNLOAD:
choice = 'save';
Expand Down
15 changes: 0 additions & 15 deletions extension/external-viewers.js

This file was deleted.

4 changes: 4 additions & 0 deletions extension/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
domProp = pair[0].trim();
i18n_id = pair[1].trim();
}
if (i18n_id === 'brandShortName') {
setBrandedTranslation(element, domProp, 'brandShortName');
continue;
}
var translation = chrome.i18n.getMessage(i18n_id);
if (translation) {
if (translation.includes('brandShortName')) {
Expand Down
2 changes: 0 additions & 2 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"scripts": [
"mime-metadata.js",
"prefs.js",
"external-viewers.js",
"dialogmanager.js",
"open-with.js",
"content-handlers-firefox.js",
"background.js"
]
Expand Down
85 changes: 0 additions & 85 deletions extension/open-with.js

This file was deleted.

7 changes: 0 additions & 7 deletions extension/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,11 @@ <h2>Your saved MIME actions</h2>
</table>
</div>

<h2>"Open with" viewers</h2>
External viewers allows you to display many other files directly in the browser.<br>

<div class="row" id="external-viewers">
</div>

<footer>
&copy; 2013 - 2017 Rob Wu &lt;[email protected]&gt;
&bull; <a href="https://github.com/Rob--W/open-in-browser">Source code on Github</a>
</footer>
<script src="prefs.js"></script>
<script src="external-viewers.js"></script>
<script src="options.js"></script>
</body>
</html>
39 changes: 3 additions & 36 deletions extension/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals Prefs, MimeActions, EXTERNAL_VIEWERS */
/* globals Prefs, MimeActions */
'use strict';
Prefs.init();
var $ = document.getElementById.bind(document);
Expand Down Expand Up @@ -67,7 +67,8 @@ function renderMimeMappingsCommon(mimeMappings, table, isSniffingMimeType) {
} else if (mimeAction.action === MimeActions.OIB_SERVER_SNIFF) {
actionMessage = 'Open in browser with sniffed MIME';
} else if (mimeAction.action === MimeActions.OPENWITH) {
actionMessage = 'Open with ' + EXTERNAL_VIEWERS[mimeAction.openWith].name;
// TODO: i18n.
actionMessage = 'Open with browser (Choose other Application)';
} else if (mimeAction.action === MimeActions.DOWNLOAD) {
actionMessage = 'Save file';
}
Expand All @@ -90,38 +91,6 @@ function renderMimeMappingsCommon(mimeMappings, table, isSniffingMimeType) {
table.hidden = false;
}

function renderViewerPreferences(externalViewersPref) {
var prefItems = document.createDocumentFragment();

var labelBase = document.createElement('label');
labelBase.className = 'pref';

var checkboxBase = document.createElement('input');
checkboxBase.type = 'checkbox';

Object.keys(EXTERNAL_VIEWERS).forEach(function(identifier) {
var viewer = EXTERNAL_VIEWERS[identifier];
var pref = externalViewersPref[identifier];
var isEnabled = !pref || pref.enabled;
var label = labelBase.cloneNode();
var checkbox = checkboxBase.cloneNode();

checkbox.checked = isEnabled;
checkbox.onchange = function toggleViewer() {
if (!pref) pref = externalViewersPref[identifier] = {};
pref.enabled = this.checked;
Prefs.set('external-viewers', externalViewersPref);
};

var labelText = viewer.name;

label.appendChild(checkbox);
label.appendChild(document.createTextNode(labelText));
prefItems.appendChild(label);
});
$('external-viewers').appendChild(prefItems);
}

bindBooleanPref('text-nosniff');

bindBooleanPref('octet-sniff-mime');
Expand All @@ -130,5 +99,3 @@ Prefs.setPrefHandler('mime-mappings', renderMimeMappings);

Prefs.setPrefHandler('sniffed-mime-mappings', renderSniffedMimeMappings);

Prefs.setPrefHandler('external-viewers', renderViewerPreferences);

8 changes: 1 addition & 7 deletions extension/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ var prefs = {
'mime-mappings': {},
// Similar to mime-mappings; used if MIME-sniffing is enabled.
'sniffed-mime-mappings': {},
'external-viewers': {
// Formats: (see open-with.js)
// 'google_docs': { "enabled": true },
// 'pdfjs': { "enabled": true, "extensionid": ''...' },
},
// Whether to add the "X-Content-Type-Options: nosniff" header to text/plain requests
'text-nosniff': true,
// Whether to use the file extension for detecting type when mime=application/octet-stream
Expand Down Expand Up @@ -134,7 +129,6 @@ function getMimeAction(mimeType, isSniffingMimeType, serverSentMimeType) {
case MimeActions.OPENWITH:
return {
action: actionType,
openWith: actionArgs
};
case MimeActions.DOWNLOAD:
return {
Expand All @@ -156,7 +150,7 @@ function setMimeAction(mimeType, isSniffingMimeType, desiredAction) {
var actionType = desiredAction.action;
// The following assumes that the desiredAction object is always clean,
// i.e. it doesn't contain any significant properties of a different action.
var actionArgs = desiredAction.openWith || desiredAction.mime || '';
var actionArgs = desiredAction.mime || '';
if (actionType === MimeActions.OIB_SERVER_SENT ||
actionType === MimeActions.OIB_SERVER_SNIFF) {
// For these actions the MIME is inferred from the request.
Expand Down
10 changes: 9 additions & 1 deletion gen-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var SOURCES = [{
'intro2',
'from',
'actionQuestion',
//'openWith', // Future update
'openWith',
'other',
'saveFile',
'rememberChoice',
Expand Down Expand Up @@ -122,6 +122,14 @@ var SOURCES = [{
aliases: {
'preferencesDefaultTitleMac.title': 'preferences'
}
}, {
source: '/chrome/mozapps/handling/handling.dtd',
properties: [
'chooseOtherApp'
],
aliases: {
'ChooseOtherApp.description': 'chooseOtherApp',
},
}];

/**
Expand Down
Loading

0 comments on commit 3c1c1df

Please sign in to comment.