简体   繁体   English

javascript运行网站功能

[英]running site function from javascript

thats what i got so far 多数民众赞成在我到目前为止

function onReady(callback) {
    if (document.readyState === "complete") window.setTimeout(callback, 0);
    else window.addEventListener("load", callback, false);
}
onReady(function() {
    removeit("new");
});

function removeit() {
    el = document.querySelector("#toolbarCurtain");
    if (el) {
        el.style.setProperty("display", "", null);
    } * * EXEC COMMAND
    return;
}

the " **EXEC COMMAND" thing is where i want to call a function that is in the site itself. “ ** EXEC COMMAND”是我要调用站点本身中的函数的地方。 the function is called drawPlayer(), and when i type drawPlayer() in the chrome console it works. 该函数称为drawPlayer(),当我在chrome控制台中键入drawPlayer()时,它将起作用。 i can't make my script call the function, how do i do that? 我无法使我的脚本调用该函数,该怎么办?

Taken from here . 取自这里

All injected code runs in an isolated world, a javascript context isolated from the main website's JS context. 所有注入的代码都在一个隔离的世界中运行,这是一个与主要网站的JS上下文隔离的javascript上下文。 This is a built-in security measure and cannot be disabled. 这是内置的安全措施,不能禁用。

The solution is to append a script. 解决方案是添加脚本。 If you need a lot of executions, you should normally setup a custom communication event, and only inject one script listening to that custom event and eval() ing the event data. 如果需要大量执行,通常应设置一个自定义通信事件,并且只注入一个脚本来监听该自定义事件并通过eval()来获取事件数据。 Here is how Google recommends to do this. Google建议这样做的方法如下。

Bear in mind this will only work if the target site doesn't have CSP (Content-Security-Policy). 请记住,这仅在目标站点没有CSP(Content-Security-Policy)时有效。

var RunInThisContext = function(c){ 
    var code = document.createTextNode(c);
    var script = document.createElement('script');
    script.type='text/javascript';
    script.appendChild(code);
    document.body.appendChild(script);      
}; 

Use like this: 像这样使用:

RunInThisContext('('+(function(){ 
     /////////////////////
     //
     //   RUN YOUR FUNCTIONS HERE
     //
     /////////////////////
}).toString()+'()); '); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM