简体   繁体   English

浏览器控制台中的Javascript要求

[英]Javascript Require in browser console

When entering javascript the in the browsers console window: 在浏览器控制台窗口中输入javascript时:

1) 1)

The following code works: 以下代码有效:

alert('hi');

2) 2)

The following doesn't: 以下不是:

(function() {
var scr = document.createElement('script');
scr.src = 'http://www.myrandomwebsite.com/myjs.js';
document.head.appendChild(scr);
myfunc();
})()

where the myjs.js folder contains: myjs.js文件夹包含:

var myfunc = function(){alert('hi')};

Explaining 2) the snippet of code entered in this case does cause the following code to appear at the end of the header tag in the source code: 解释2)在这种情况下输入的代码片段确实导致以下代码出现在源代码的标头标记的末尾:

<script src="http://www.myrandomwebsite.com/myjs.js"></script>

but the function myfunc isn't being recognized, because I get the following error message: 但是函数myfunc无法识别,因为我收到以下错误消息:

VM132:5 Uncaught ReferenceError: myfunc is not defined VM132:5未捕获的ReferenceError:未定义myfunc

Which leads me to believe that as a security measure, browsers don't run javascript code that is edited after the page has loaded, or something along those lines. 这使我相信,作为一种安全措施,浏览器不会运行在页面加载后编辑的javascript代码或类似的代码。

QUESTION: Is there a workaround of some sorts here? 问题:这里有某种解决方法吗? Whether it be a function that works like require('...'); 是否它是一个像require('...'); in most programming languages. 在大多数编程语言中。 but It can't be by installing any special extension, just something that works on the fly. 但这不能通过安装任何特殊的扩展程序来实现,而只能通过即时运行而实现。

I move around a lot, and work on different computers, and would love to be able to use some of the code I have without needing to always carry a USB with me. 我经常走动,并在不同的计算机上工作,并且希望能够使用我拥有的某些代码,而不必总是随身携带USB。

________________________ UPDATE ________________________ ________________________ 更新 ________________________

The solution @Jared Smith proposed worked perfectly. @Jared Smith提出的解决方案效果很好。 Now there is one instance where it doesn't work (and I understand why). 现在有一个实例,它不起作用(我明白为什么)。 When a site has a Content-Security-Policy that doesn't allow scripts to be loaded from other URLs or any connections to be loaded: 如果站点的内容安全策略不允许从其他URL加载脚本或任何连接加载,则:

for instance: 例如:

Content-Security-Policy: script-src 'self' https://apis.google.com
Content-Security-Policy: connect-src 'self' https://apis.google.com

And this makes perfect sense. 这是完全合理的。 My situation is that I have my own code that I run, however I wanted to be able to have this code stored on another site, and use it when I travel or am on other computers, without the need of extracting them from a USB. 我的情况是我有自己运行的代码,但是我希望能够将此代码存储在另一个站点上,并在旅行或在其他计算机上使用时使用,而无需从USB中提取它们。

Now I understand sites whitelisting what sources scripts are loaded from for security reasons, but I don't understand why there isn't an exception when it is done by the developers console . 现在,我了解站点出于安全原因将加载源脚本的白名单列入白名单,但是我不明白为什么开发者控制台完成后不会出现异常。 CSP is used mostly to prevent XSS attacks and such, but someone who is in the console is purposely using code and testing features. CSP主要用于防止XSS攻击等,但是控制台中的某人故意使用代码和测试功能。

QUESTION: Is there some feature in the console that might allow for scripts to be loaded from an alternative site? 问题:控制台中是否有某些功能可以允许从备用站点加载脚本? I have been fiddling with the Source Script Snippets but haven't been able to find an option. 我一直在摆弄源脚本片段,但一直找不到选项。 Or might there be some other unrelated work around? 还是可能有其他不相关的工作?

this works: 这有效:

var scr = document.createElement('script');
scr.text = 'function sayHi() { alert("hi")}';
document.head.appendChild(scr);
sayHi()

...aaaaaand this works: ... aaaaaa,这可行:

var scr = document.createElement('script');
scr.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js';
scr.onload = function() {
  $(function(){
    $('body').css({backgroundColor: 'red'});
  });
}
document.head.appendChild(scr);

A bit of an explanation is in order here. 这里需要进行一些解释。

When you add a script tag with a src dynamically the browser will fire off a request for the JavaScript file. 动态添加带有src的脚本标签时,浏览器将触发对JavaScript文件的请求。 But this operation, unlike a regular tag initially on the page, is asynchronous , it doesn't block. 但是,此操作与页面上最初的常规标签不同,它是异步的 ,不会阻塞。 What this means is that the next line of code (in your case a call to myfunc ) gets executed immediately, while the file is still being fetched. 这意味着下一行代码(在您的情况下为myfunc调用)将立即执行,而文件仍在获取中。 So if you want to defer code execution until after that script has been fetched, parsed, and executed, you need to register a callback. 因此,如果您希望将代码执行推迟到获取,解析和执行该脚本之后,则需要注册一个回调。 We can do this by listening for the script element's load event. 我们可以通过侦听脚本元素的load事件来做到这一点。 There are two ways: 有两种方法:

scr.addEventListener('load', function() { myfunc(); });

and assigning the function to the script elements onload property. 并将功能分配给脚本元素的onload属性。 The only real difference is that the addEventListener way allows you to attach multiple listeners, where as scr.onload can only have one value (like any other object property). 唯一真正的区别是addEventListener方式允许您附加多个侦听器,因为scr.onload只能具有一个值(就像其他任何对象属性一样)。

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

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