17

Tôi đang tạo tiện ích mở rộng chrome tuy nhiên dường như tôi gặp lỗi sau khi cố gắng kích hoạt sự kiện onclick().Cách khắc phục lỗi yêu cầu JavaScript nội dòng mở rộng chrome?

Refused to load the script 'https://apis.google.com/js/client.js?onload=handleClientLoad' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:" 

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution. 

Đây là manifest.json tôi:

{ 
    "manifest_version": 2, 

    "name": "SECURE", 
    "description": "this extension offers secure communication for GMAIL  users", 
    "version": "1.0", 

"browser_action": { 
"default_icon": "resources/icon16.png", 
"default_popup": "popup.html", 
"default_title": "Click here!" 


}, 

"background":{ 
    "scripts":["background.js"] 
}, 

"content_scripts": [ 
    { 
    "matches": ["http://*/*", "https://*/*"], 
    "js":["myscript.js"], 
    "run_at": "document_end" 
    } 
    ], 
"permissions": ["identity", "https://accounts.google.com/*", "https://www.googleapis.com/*"], 

"oauth2": { 
    "client_id": "975410329966.apps.googleusercontent.com", 
"scopes": [ 
    "<all urls>", 
    "https://www.googleapis.com/auth/drive", 
    "https://mail.google.com/", 
    "https://www.googleapis.com/auth/gmail.login", 
    "https://www.googleapis.com/auth/gmail.compose", 
    "https://www.googleapis.com/auth/gmail.readonly", 
    "https://www.googleapis.com/auth/gmail.send" 
    ], 

"content_security_policy":"script-src 'self' 'unsafe-inline' 'unsafe eval' https://apis.google.com/js/client.js?; object-src 'self'" 


} 
} 

Bất kỳ sự giúp đỡ đối với sửa chữa lỗi này sẽ rất được đánh giá cao.

+2

trùng lặp có thể xảy ra của [Chrome mở rộng cửa sổ bật lên không hoạt động, nhấp vào các sự kiện không được xử lý] (https://stackoverflow.com/questions/17601615/the-chrome-extension-popup-is-not-working- nhấp-sự kiện-không-được xử lý) – Makyen

Trả lời

8

Theo mặc định Content Security Policy, tập lệnh nội tuyến sẽ không được tải và chỉ có thể tải tập lệnh cục bộ. Bạn có thể thư giãn chính sách mặc định bằng cách:

  1. Inline Script. Hãy xem Official Guide, tập lệnh nội tuyến có thể được đưa vào danh sách cho phép bằng cách chỉ định mã băm được mã hóa base64 của mã nguồn trong chính sách. Xem Hash usage for elements để biết ví dụ.

    Nhưng tôi tin rằng cách tốt hơn sẽ trích xuất logic này thành tập lệnh riêng và không sử dụng tập lệnh nội tuyến.

  2. Tập lệnh từ xa. Bạn có thể tạo danh sách trắng nguồn kịch bản https://apis.google.com/js/client.js?onload=handleClientLoad bởi phần sau trong manifest.json

    "content_security_policy":"script-src 'self' https://apis.google.com; object-src 'self'" 
    

    Ngoài ra, tôi tin rằng một cách tốt hơn có thể được tải về từ xa client.js và bao gồm nó như là một kịch bản của địa phương.

Xin lưu ý theo mô tả của Inline Script, unsafe-inline không còn hoạt động.

Cho đến Chrome 45, không có cơ chế để thư giãn hạn chế đối với việc thực thi JavaScript nội tuyến. Cụ thể, s đặt chính sách tập lệnh bao gồm 'không an toàn-inline' sẽ không có hiệu lực.

Các vấn đề liên quan