我写了一个GreaseMonkey脚本来为blogger.com的HTML视图添加一个按钮。我有警报到处,所以我知道一切都在运行。脚本触发但没有结果
奇怪的是,它发射两次,每次都略有不同。 当我导航到blogger.com帖子编辑时,脚本首先在黑色页面上触发,并以正确的顺序(包括“内部”顺序)遍历所有警报。
在页面加载后的第二次运行中,除“内部”之外的每个警报都会运行并且不会产生错误。
// ==UserScript==
// @name Blogger: Remove HTML Space
// @include http://www.blogger.com/blogger.g?blogID*editor/target*\
// ==/UserScript=
function contentEval(source) {
// Check for function input.
alert("asdas");
if ('function' == typeof source) {
// Execute this function with no arguments, by adding parentheses.
// One set around the function, required for valid syntax, and a
// second empty set calls the surrounded function.
source = '(' + source + ')();'
}
alert("asdas2");
// Create a script node holding this source code.
var script = document.createElement('script');
alert("asdas3");
script.setAttribute("type", "application/javascript");
alert("asdas4");
script.textContent = source;
alert("asdas5");
// Insert the script node into the page, so it will run, and immediately
// remove it to clean up.
alert("asdas6");
document.body.appendChild(script);
alert("asdas7");
}
alert("test")
contentEval(function(){ alert("inside"); document.getElementById("postingHtmlToolbar").firstChild.innerHTML += '<div id="SP" class="goog-inline-block goog-toolbar-button" title="SP" role="button" style="-moz-user-select: none;"><div class="goog-inline-block goog-toolbar-button-outer-box"><div class="goog-inline-block goog-toolbar-button-inner-box"><button type="button" onclick="document.getElementById(\'postingHtmlBox\').value = document.getElementById(\'postingHtmlBox\').value.replace(/&nbsp;/g, \' \');"><b>SP</b></button></div></div></div>'});
alert("testw");
blogger.com是否有反脚本注入的东西?
有趣的是,如果我的脚本为什么没有做它应该做的事情,我还是不知道。 总而言之,我不关心脚本最终是否会触发100次。 您是否认为问题可能只是由于多次运行而发生? – Jonathon
我之前并没有在脚本中详细看过,请查看编辑内容,准确地说出发生了什么以及发生了什么。问题是追加HTML或点击按钮? 'document.getElementById(“postingHtmlToolbar”)。firstChild.innerHTML + =“FOO”'工作吗? 关于多次发射,我认为每次执行都在单独的页面上下文中,所以它们不应该互相影响(但我不完全确定)。 –
@edit:我不认为greasemonkey可以做这样的事情,它不能直接编辑页面。 不要问我为什么通用汽车公司不能以简单的方式来运行你100%信任的代码,但出于安全原因,我不明白他们限制你可以做的事情。 – Jonathon