在Chrome中进行测试时,Chrome扩展程序无法运行:// extensions / [英] Chrome extension don't work when tested in chrome://extensions/
问题描述
我想了解一些关于google-extensions的知识,所以我试着做一个简单的扩展,使用<$ c向 api
发出请求
当我直接在浏览器(localhost)中测试时,它可以很好地工作,但是当我尝试要在开发人员模式下将其添加到 chrome:// extensions /
并点击加载未打包的扩展名... ,该功能无效。
这是我的 app.js
,其中 ajax
调用:
document.addEventListener('DOMContentLoaded',function(){
$('#form-custom') .on('submit',function(event){
event.preventDefault();
user = $('#user')。val();
$。 ajax({
url:'https://sitewiththeapi.net/api/searchuser/'+user+'/show/list.json',
dataType:'jsonp'
})
.done(函数(数据){
console.log(data);
})
.fail(函数(jqXHR,textStatus,errorThrown){
console.error(jqXHR,textStatus,errorThrown);
})
}
},false);
我的 manifest.json
file:
{
添加到
manifest_version:2,
name:MyExtension,
description:此扩展名将用于学习,
version:1.0,
browser_action:{
default_icon: icon.png,
default_popup:index.html
},
content_scripts:[
{
matches:[
< all_urls>
],
js:[jquery.min.js,app.js]
}
],
permissions:[
activeTab,
http:// * /,
https:// * /
]
}
$ c编辑:https manifest.json
文件,错误仍然存在。
检查弹出窗口我收到以下错误:
jquery.min.js:4拒绝加载脚本
' https://sitewiththeapi.net/api/searchuser/theuser/show/ list.json '
,因为它违反了以下内容安全策略指令:
script-src'self'blob:filesystem:chrome-extension-resource:。
解决方案尝试添加:
converted_from_user_script:true,
在manifest.json中的版本之后。
这不仅仅是一种合法的解决方案,而且如果您正在为自己开发扩展程序或仅仅是为了学习,这很好。
I'm trying to learn a little a bit about google-extensions, so I tried to make a simple extension that make a request to an
api
usingajax
then return some results.When I test directly in the browser(localhost), it works perfectly, but when I try to test adding it to
chrome://extensions/
and clicking on Load unpacked extension… in developer mode, it's not working.This is my
app.js
withajax
call:document.addEventListener('DOMContentLoaded', function() { $( '#form-custom' ).on( 'submit', function( event ) { event.preventDefault(); user = $('#user').val(); $.ajax({ url:'https://sitewiththeapi.net/api/searchuser/'+user+'/show/list.json', dataType:'jsonp' }) .done(function(data){ console.log(data); }) .fail( function(jqXHR, textStatus, errorThrown){ console.error(jqXHR, textStatus, errorThrown); }) } }, false);
And my
manifest.json
file:{ "manifest_version": 2, "name": "MyExtension", "description": "This extension will for learning", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "index.html" }, "content_scripts": [ { "matches": [ "<all_urls>" ], "js": ["jquery.min.js", "app.js"] } ], "permissions": [ "activeTab", "http://*/", "https://*/" ] }
EDIT: Added
https
tomanifest.json
file and the error persists. Inspecting the popup I got the following error:jquery.min.js:4 Refused to load the script 'https://sitewiththeapi.net/api/searchuser/theuser/show/list.json' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
解决方案Try adding:
"converted_from_user_script": true,
After version in your manifest.json.
This is more of a hack than a legit solution but its fine if you are developing that extension for yourself or just to learn.
这篇关于在Chrome中进行测试时,Chrome扩展程序无法运行:// extensions /的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!