0

所以我正在开发一个通过 goo.gl 缩短 URL 的 chrome 扩展

这是我的实际代码,如果它可能有帮助:

 $("#ajaxfiller").text("");
 //Get URL entered
 var longUrl = $("#input2").val();
 longUrl = '"' + longUrl + '"';


 function googlurl(url, cb) {
     jsonlib.fetch({
         url: 'https://www.googleapis.com/urlshortener/v1/url',
         header: 'Content-Type: application/json',

         // Just a static URL for shortening:

         data: JSON.stringify({
             longUrl: "https://google.com"
         })
     }, function (m) {
         var result = null;
         try {
             result = JSON.parse(m.content).id;
             if (typeof result != 'string') result = null;
         } catch (e) {
             result = null;
         }
         cb(result);
     });


 }

 googlurl(longUrl, function (s) {
     alert(s);
     $("#kinchyj").show();
     $("#ajaxfiller").val(s);
 });

错误

注意:当我将扩展程序加载popup.html为常规网页时,它确实有效。

看来此错误是 Chrome 安全措施。无论如何我可以在清单中将其列入白名单吗?

4

1 回答 1

0

manifest.json您可以使用此处content_security_policy详细说明的方式修改安全策略。你需要的是:

"content_security_policy": "script-src 'self' https://www.googleapis.com/urlshortener/v1/url; object-src 'self'"

如果没有看到 HTML,我无法判断,但最好将所有 JavaScript 移动到通过标签包含的本地外部文件中。<script>

于 2013-02-11T18:16:10.830 回答