是否可以修改页面的JavaScript然后重新加载页面而不重新加载修改后的JavaScript文件(从而失去修改)?
我正在尝试window.location使用Greasemonkey在文档的开头有选择地禁用它.
我不想完全禁用javascript,只是禁用一些使用javascript完成的重定向.它们看起来像这样:
window.location = "unwanted url";
Run Code Online (Sandbox Code Playgroud) 我在HTML的头部访问了一个带有javascript文件的网站
<script language="javascript" src="javscript.js"></script>
Run Code Online (Sandbox Code Playgroud)
该文件中的代码是:
// keypress management
if (document.layers) document.captureEvents(Event.KEYPRESS)
function update(e) {
if (document.all) { // Explorer
if (event.keyCode==13) document.forms[0].submit(); // 13 = ENTER
else if (event.keyCode==26) runHelp(hplk); // 26 = CTRL+Z
return;
} else { // mozilla
if (e.which==13) document.forms[0].submit(); // 13 = ENTER
else if (e.which==26) runHelp(hplk); // 122 = CTRL+Z
return;
}
}
document.onkeypress=update;
Run Code Online (Sandbox Code Playgroud)
我想用Greasemonkey禁用/删除/替换此功能.
我试过了
unsafeWindow.update = function(){}
Run Code Online (Sandbox Code Playgroud)
没有结果!(在控制台中没有错误)
有没有办法杀死这个功能?
I'm looking for modify a javascript game with an userscript.
The problem is that all the game code is wrapped with a anonymous function like this
(function () { "use strict";
var js = 'test';
})();
Run Code Online (Sandbox Code Playgroud)
Can I have access to a JS variable with my userscript?
Edit :
See also : How to alter this javascript with Greasemonkey?
This question is not the same as Is it possible to gain access to the closure of a function? !
我正在使用我无法控制的HTML页面.它在内联<script>标记中定义了一个Javascript函数,并将其调用<body onload="...">:
<html>
...
<body onload="init()">
<script type="text/javascript" language="javascript">
function init() {
...
}
</script>
...
Run Code Online (Sandbox Code Playgroud)
如何在调用之前更改该功能?我曾尝试使用Greasemonkey修改脚本或在其后插入另一个脚本来覆盖该函数,但它似乎没有任何效果.
我想更改网页显示的部分内容,是否可以用usercript覆盖一个函数?
下面是我想覆盖的功能(也可以在这里找到):
function StartDrawing() {
if ((typeof (systemsJSON) != "undefined") && (systemsJSON != null)) {
var stellarSystemsLength = systemsJSON.length;
$('#mapDiv').empty();
if (stellarSystemsLength > 0) {
InitializeRaphael();
var i = 0;
for (i = 0; i < stellarSystemsLength; i++){
var stellarSystem = systemsJSON[i];
DrawSystem(stellarSystem)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许我运行自己的绘图算法.
如何使用chrome(或Firefox)中的用户脚本执行此操作?
一个网页正在将一个内置的 javascript 方法设置为null,我正在尝试找到一种方法来调用用户脚本中的重写方法。
考虑以下代码:
// Overriding the native method to something else
document.querySelectorAll = null;
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试执行document.querySelectorAll('#an-example'),我会得到异常Uncaught TypeError: null is not a function。原因是该方法已更改为null并且不再可访问。
我正在寻找一种方法来以某种方式恢复对用户脚本中方法的引用。问题是网站可以覆盖对任何东西的引用(甚至包括Document,Element和Object构造函数)。
由于该网站还可以方便地设置参考null,我需要一种方法来找到一种方法来访问querySelectorAll方法的网站将无法覆盖。
挑战在于,在我的用户脚本在页面上执行时,任何方法(例如createElement和getElementsByTagName(除了它们的prototypes))都可以被覆盖null。
我的问题是,如果它们也被覆盖,我该如何访问Document或HTMLDocument构造函数方法?
由于浏览器限制, Tampermonkey无法在文档的开头运行我的脚本,因此我无法保存对我想要使用的方法的引用,如下所示:
// the following code cannot be run at the beginning of the …Run Code Online (Sandbox Code Playgroud) 假设有一个站点在html脚本标记中包含一个外部.js文件,如下所示:
<script src="somescript.js">
Run Code Online (Sandbox Code Playgroud)
我希望lublessmonkey拦截每个这样的脚本,并在执行之前更改其中的某些值。例如,我想将其中所有出现的值“ 400”更改为“ 300”,然后继续加载页面,就像脚本使用这些值而不是原始值一样。目前,我在油脂单中使用以下代码:
function replaceTargetJavascript (scriptNode) {
var scriptSrc = scriptNode.textContent;
scriptSrc = scriptSrc.replace (
/'400'/,
"'300'"
);
addJS_Node (scriptSrc);
}
document.addEventListener("beforescriptexecute", function(e) {
checkForBadJavascripts ( [
[false, /'400'/, replaceTargetJavascript]
] );
}, true);
Run Code Online (Sandbox Code Playgroud)
根据我的消息来源,哪种方法是正确的方法,但没有用。谁能帮我解决这个问题?
javascript ×8
greasemonkey ×4
userscripts ×3
function ×1
object ×1
overriding ×1
prototype ×1
tampermonkey ×1