Add window for running arbitrary privileged JavaScript

This is a replacement for the Execute JS extension that could be used
with Zotero for Firefox.

To enable, go to the Config Editor in the Advanced pane of the
preferences and set devtools.chrome.enabled to true, and then restart
Zotero. A "Run JavaScript" option will appear in the Tools menu.
This commit is contained in:
Dan Stillman 2019-01-22 00:10:14 -05:00
parent eb5d28b626
commit f918e27e46
6 changed files with 117 additions and 0 deletions

View file

@ -0,0 +1,26 @@
<html>
<head>
<link href="chrome://zotero/skin/runJS.css" rel="stylesheet"/>
</head>
<body>
<nav>
<button onclick="run()">Run</button>
<span id="run-label"/>
</nav>
<main>
<div class="textbox-container">
<label for="code">Code:</label>
<textarea id="code"></textarea>
</div>
<div class="textbox-container">
<label for="result">Result:</label>
<textarea id="result" readonly></textarea>
</div>
</main>
<script src="include.js"></script>
<script src="runJS.js"></script>
</body>
</html>

View file

@ -0,0 +1,32 @@
function run() {
var win = Zotero.getMainWindow();
if (!win) {
return;
}
var code = document.getElementById('code').value;
var result = win.eval(code);
document.getElementById('result').value = Zotero.Utilities.varDump(result);
}
window.addEventListener('keypress', function (event) {
if (Zotero.isMac) {
if (!event.metaKey) {
return;
}
}
else if (!event.ctrlKey) {
return;
}
if (event.shiftKey || event.altKey) {
return;
}
if (event.key == 'r') {
run();
event.stopPropagation();
}
});
var shortcut = Zotero.isMac ? 'Cmd-R' : 'Ctrl+R';
document.getElementById('run-label').textContent = `(${shortcut})`;

View file

@ -51,6 +51,10 @@ const ZoteroStandalone = new function() {
if (Zotero.Prefs.get('devtools.errorconsole.enabled', true)) {
document.getElementById('menu_errorConsole').hidden = false;
}
if (Zotero.Prefs.get('devtools.chrome.enabled', true)) {
document.getElementById('menu_errorConsole').hidden = false;
document.getElementById('menu_runJS').hidden = false;
}
document.getElementById('key_copyCitation')
.setAttribute('key', Zotero.Keys.getKeyForCommand('copySelectedItemCitationsToClipboard'));
@ -450,6 +454,10 @@ function toJavaScriptConsole() {
toOpenWindowByType("global:console", "chrome://global/content/console.xul");
}
function openRunJSWindow() {
window.open('chrome://zotero/content/runJS.html', 'run-js', 'width=900,height=700,resizable');
}
function toOpenWindowByType(inType, uri, features)
{
var topWindow = Services.wm.getMostRecentWindow(inType);

View file

@ -198,6 +198,7 @@
<menuitem id="menu_addons" label="&addons.label;"
oncommand="Zotero.openInViewer('chrome://mozapps/content/extensions/extensions.xul', ZoteroStandalone.updateAddonsPane)"/>
<menuitem id="menu_errorConsole" label="Error Console" oncommand="toJavaScriptConsole()" hidden="true"/>
<menuitem id="menu_runJS" label="Run JavaScript" oncommand="openRunJSWindow()" hidden="true"/>
</menupopup>
</menu>

View file

@ -62,6 +62,10 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
this.initialURL; // used by Schema to show the changelog on upgrades
this.Promise = require('resource://zotero/bluebird.js');
this.getMainWindow = function () {
return Services.wm.getMostRecentWindow("navigator:browser");
};
this.getActiveZoteroPane = function() {
var win = Services.wm.getMostRecentWindow("navigator:browser");
return win ? win.ZoteroPane : null;

View file

@ -0,0 +1,46 @@
body {
font-family: Helvetica, sans-serif;
margin: 0;
height: 100%;
}
nav {
padding: 8px 5px 5px;
height: 25px;
}
button {
font-size: 15px;
}
#run-label {
margin-left: 2px;
font-size: 12px;
}
main {
display: flex;
justify-content: stretch;
height: calc(100% - 38px);
}
.textbox-container {
display: flex;
flex-direction: column;
align-items: stretch;
flex-grow: 1;
font-size: 14px;
padding: 10px;
}
label {
flex-grow: 0;
}
textarea {
flex-grow: 1;
margin: 5px 0;
font-family: Monaco, Consolas, Inconsolata, monospace;
font-size: 12px;
resize: none;
}