Skip to content

Commit

Permalink
Merge branch 'install-dialog'
Browse files Browse the repository at this point in the history
  • Loading branch information
arantius committed Jan 9, 2018
2 parents 8b5b650 + 5295db5 commit 438fb44
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
7 changes: 7 additions & 0 deletions doc/Messages.md
Expand Up @@ -114,6 +114,13 @@ Response data:

* An array of `.details` objects from installed `RunnableUserScript`s.

# OpenInstallDialog
Sent by: `content/script-detect.js`

Sent when the content script has detected navigation to a user script.

* `userScript` Details of parsing the user script for installation.

# InstallProgress
Sent by: `bg/user-script-install.js`
Received by: `content/install-dialog.js`
Expand Down
1 change: 1 addition & 0 deletions manifest.json
Expand Up @@ -26,6 +26,7 @@
"src/bg/execute.js",
"src/bg/is-enabled.js",
"src/bg/on-message.js",
"src/bg/on-open-install-dialog.js",
"src/bg/on-user-script-notification.js",
"src/bg/on-user-script-open-in-tab.js",
"src/bg/on-user-script-xhr.js",
Expand Down
12 changes: 8 additions & 4 deletions src/bg/on-message.js
Expand Up @@ -14,10 +14,14 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
return;
}

// Messages named "Api*" can come from anywhere (i.e. _content_ scripts, where
// we execute user scripts). These are handlers for the GM APIs. Otherwise,
// only accept messages coming from our own source prefix.
if (!message.name.startsWith('Api') && !sender.url.startsWith(myPrefix)) {
if (message.name == 'OpenInstallDialog') {
// No-op; content can legitimately ask for the dialog to be opened.
} else if (
!message.name.startsWith('Api') && !sender.url.startsWith(myPrefix)
) {
// Messages named "Api*" can come from anywhere (i.e. _content_ scripts,
// where we execute user scripts). These are handlers for the GM APIs.
// Otherwise, only accept messages coming from our own source prefix.
throw new Error(
`ERROR refusing to handle "${message.name}" `
+ `message from sender "${sender.url}".`);
Expand Down
16 changes: 16 additions & 0 deletions src/bg/on-open-install-dialog.js
@@ -0,0 +1,16 @@
/*
This file is responsible for opening the dialog for user script installation.
*/

function onOpenInstallDialog(message, sender, sendResponse) {
// TODO: Compatibility with Firefox for Android, which does not have the
// "windows" API.
chrome.windows.create({
'height': 640,
'titlePreface': message.userScript.name + ' - Greasemonkey User Script',
'type': 'popup',
'url': chrome.runtime.getURL('src/content/install-dialog.html')
+ '?' + escape(JSON.stringify(message.userScript)),
'width': 480,
});
}
7 changes: 7 additions & 0 deletions src/content/install-dialog.js
Expand Up @@ -87,4 +87,11 @@ window.addEventListener('DOMContentLoaded', event => {
rvDetails.iconUrl = rvDetails.iconUrl || "";

rivets.bind(document.body, rvDetails);

// Fix for Fx57 bug where bundled page loaded using
// browser.windows.create won't show contents unless resized.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1402110
chrome.windows.getCurrent(win => {
chrome.windows.update(win.id, {width: win.width + 1});
})
});
11 changes: 7 additions & 4 deletions src/content/script-detect.run.js
@@ -1,6 +1,6 @@
/*
This file detects navigation events. If a navigation points to a user script,
the installation dialog is fired.
the installation dialog is opened.
*/

// Private implementation.
Expand All @@ -20,9 +20,12 @@ if (document.contentType in userScriptTypes) {
var userScriptContent = document.body.textContent;
var userScriptDetails = parseUserScript(userScriptContent, userScriptUrl);

let installUrl = chrome.runtime.getURL('src/content/install-dialog.html')
+ '?' + escape(JSON.stringify(userScriptDetails));
location.replace(installUrl);
chrome.runtime.sendMessage({
'name': 'OpenInstallDialog',
'userScript': userScriptDetails,
});

history.back();
}

})();

0 comments on commit 438fb44

Please sign in to comment.