简体   繁体   English

无法从另一台服务器加载脚本-内容安全策略问题?

[英]Unable to load script from another server - Content Security Policy issue?

I have been trying to turn a bookmarklet into a small development environment that I can use for testing some javascript and sending commands easily on the fly and updating the code on my server quickly to see the result. 我一直在尝试将小书签变成一个小型开发环境,该环境可用于测试一些javascript并即时轻松地发送命令并快速更新服务器上的代码以查看结果。 This has half way worked using method's I have found in this site and google however it doesn't seem to work very well and sometimes randomly doesn't work. 使用我在本网站和google中找到的方法,这种方法已经完成了一半,但是它似乎不能很好地工作,有时会随机地不起作用。 The end goal is to have a bookmarklet that I can click on from any page and it loads a javascript file I have saved on my server. 最终目标是拥有一个可以在任何页面上单击的书签,它可以加载保存在服务器上的javascript文件。 I have created the following two bookmarklets to try and get this working: 我创建了以下两个小书签,以尝试使其正常工作:

Failed Method 1: 方法1失败

javascript:
var s = document.createElement('script');
s.type='text/javascript';
document.body.appendChild(s);
s.src='//smewth.com/test.js';
void(0);

Method 1 in one line bookmarklet form: javascript: var s = document.createElement('script'); s.type='text/javascript'; document.body.appendChild(s); s.src='//smewth.com/test.js'; void(0); 一行书签形式的方法1: javascript: var s = document.createElement('script'); s.type='text/javascript'; document.body.appendChild(s); s.src='//smewth.com/test.js'; void(0); javascript: var s = document.createElement('script'); s.type='text/javascript'; document.body.appendChild(s); s.src='//smewth.com/test.js'; void(0);


Failed Method 2: 方法2失败:

javascript:(
 function(){
  var imported = document.createElement('script');
  imported.type='text/javascript';
  imported.src = 'https://smewth.com/test.js';
  document.head.appendChild(imported);
})();

Method 2 in one line bookmarklet form: javascript:( function(){ var imported = document.createElement('script'); imported.type='text/javascript'; imported.src = 'https://smewth.com/test.js'; document.head.appendChild(imported); })(); 方法2以一行书签的形式进行: javascript:( function(){ var imported = document.createElement('script'); imported.type='text/javascript'; imported.src = 'https://smewth.com/test.js'; document.head.appendChild(imported); })();

I got method 1 by decomposing the kickass bookmarklet from ( http://kickassapp.com/ ). 我通过从( http://kickassapp.com/ )分解kickass小书签获得了方法1。 The actual one I got from their site works fine on my browser no problems. 我从他们的网站获得的实际版本在我的浏览器上运行正常,没有问题。 I even did a direct substitution from the URL they were using to load with my URL. 我什至从他们用来加载我的URL的URL中直接进行替换。 The second method I found while searching on this site and this actually worked for a small while and stopped working for some unknown reason (maybe different browsers). 我在该网站上搜索时发现的第二种方法实际上可以使用一小会儿,但由于某种未知原因(可能是不同的浏览器)而停止了工作。 I tried appending this script object to the head and the body on each of them with no improved results. 我尝试将此脚本对象附加到每个对象的头部和身体,但没有得到改善的结果。

I created the test.js script just for this post and it contains a simple alert box statement: 我仅为该帖子创建了test.js脚本,它包含一个简单的警报框语句:

$$ [/]# cat test.js
alert("hi");
$$ [/]# 

NOTE: When I do this with the code embedded within the the bookmarklet itself without appending it to a head/body object then it works fine such as this: 注意:当我使用未嵌入书签或书签的代码将其附加到头部/身体对象时,它的工作原理如下:

javascript:%20alert("hi");

I did notice that with both of these methods, the code is actually getting injected into the page however I am not seeing the code is ever executed when I click the bookmark. 我确实注意到,使用这两种方法,代码实际上都已注入到页面中,但是单击书签时我看不到代码曾经执行过。 Does anyone know which method is the best or something similar to do this so I can have javascript load through a page which I update on a remote server (reliably)? 有谁知道哪种方法是最好的或类似的方法,这样我就可以通过在远程服务器上更新的页面(可靠地)加载javascript? Maybe I need to attach the to a different object? 也许我需要将其附加到其他对象上?

Thank you for your help. 谢谢您的帮助。

-Jeff 杰夫

UPDATE: I am showing this works while this site is loaded but it doesn't work when your at a site like google.com. 更新:我正在显示此网站加载时可以正常工作,但是当您访问google.com之类的网站时却无法正常工作。 Not sure what the difference is or how to accomodate this, google.com has a head and a body object too. 不确定差异是什么或如何适应这一点,google.com也具有头部和身体对象。 I am showing this works in some sites and in some it doesn't. 我正在某些网站上显示此功能,而在某些网站中却没有。

I figured this out. 我想通了。 There were two things occurring which accounts for the intermittent symptom of this issue. 发生了两件事,解释了此问题的间歇性症状。 The first issue was that the site which was hosting the code was on a self-signed certificate. 第一个问题是托管代码的站点位于自签名证书上。 I began to notice the issue was occurring only when trying to run this from within secure sites. 我开始注意到仅在尝试从安全站点中运行此问题时才出现此问题。 Then in Chrome I saw a error show up in the console. 然后在Chrome浏览器中,我发现控制台中显示错误。 It would be nice if Firefox gave me a error on the console or something as this was the root of the issue. 如果Firefox在控制台上给我一个错误或类似的东西,这将是问题的根源,那就太好了。 The second thing I had to do was disable OCSP in Firefox as I used a free certificate for testing purposes. 我要做的第二件事是在Firefox中禁用OCSP,因为我使用了免费证书进行测试。

I also had to use method 1 as described above. 我还必须使用如上所述的方法1。 Firefox and Chrome both did not like the anonymous function call for some reason. Firefox和Chrome出于某种原因都不喜欢匿名函数调用。 From now on I will refer to Chrome to look for errors in the console as Firefox has proven itself not very useful for this. 从现在开始,我将使用Chrome浏览器在控制台中查找错误,因为Firefox已经证明对此并不十分有用。

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

相关问题 由于内容安全策略,谷歌地图 api 脚本确实加载 - google maps api script does load due to content security policy 拒绝加载脚本,因为它违反了以下内容安全策略指令 - Refused to load the script because it violates the following Content Security Policy directive 使用内容安全策略将服务器数据传递给客户端脚本 - Passing server data to client script with Content Security Policy 包含脚本的内容安全策略 - Content security policy including a script 如何摆脱内容安全政策指令的问题? - How to get rid from the Content Security Policy directive issue? CSP,拒绝加载脚本,违反了以下内容安全策略指令:“script-src 'self'” - CSP, Refused to load the script, violates the following Content Security Policy directive: "script-src 'self'" 无法加载具有可变脚本地址的chrome newtab扩展的脚本“ Content-Security-Policy” - Can't load script 'Content-Security-Policy' for chrome newtab extension with variable script address 内容安全策略 - 仅发给一名用户 - Content Security Policy - Issue to only one user 科尔多瓦内容安全政策给登录问题 - cordova content security policy giving login issue chrome扩展名的内容安全政策问题 - content security policy issue with chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM