此脚本在Chrome和Firefox上的Tampermonkey中运行良好,但在Firefox上的Greasemonkey中不起作用。当我说它不起作用时我的意思是按钮没有出现在页面上。我使用的是Chrome 61.0.3163.79和Firefox 52.3.0。
firefox控制台显示此警告,但我不确定它是否相关:
unreachable code after return statement[Learn More] jquery-1.3.2.min.js
这是代码。在Firefox上使用greasemonkey,我在控制台中看到“创建按钮”,但没有“创建按钮”,这让我觉得按钮创建出了问题。任何帮助非常感谢!
// ==UserScript==
// @name MyScript
// @namespace MyNS
// @description Adds a button to insert text into 2 text boxes
// @version 0.1
// @include *Removed for confidentiality*
// @compatible Greasemonkey
// ==/UserScript==
var descriptionText = "myDescription";
var testingText = "myTesting";
// Check if jQuery's loaded
function GM_wait() {
if (typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; init(); }
}
// All your GM code must be inside this function
function init() {
var description = $('#description');
var testingDone = $('#testing_done');
function insertText(template, editor) {
if (template) {
if (!editor._editing) {
editor.startEdit();
}
editor._field.queue(function() {
var oldVal = editor._field.val();
editor._field.val(template + (oldVal ? "\n" : "") + oldVal);
editor._field.keyup();
editor._field.dequeue();
});
}
}
function createButton(editor) {
console.log("create button:");
editor._insertTextButton = $('<input/>')
.attr('type', 'button')
.attr('value', 'Insert CR Template')
.css('margin-left', '1em')
.click(function() {
insertText(testingText, testingDoneEditor);
insertText(descriptionText, descriptionEditor);
});
console.log("button has been created");
editor._insertTextButton.insertAfter(editor._editIcon);
}
descriptionEditor = description.data('inlineEditor');
testingDoneEditor = testingDone.data('inlineEditor');
createButton(descriptionEditor);
}
GM_wait();