Tôi đang tạo tiện ích mở rộng chrome sẽ mở tất cả các liên kết trên một trang trong tab mới.Lỗi nội dung-chính sách trong phần mở rộng của google chrome
Dưới đây là file mã của tôi:
manifest.json
{
"name": "A browser action which changes its icon when clicked.",
"version": "1.1",
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_title": "links", // optional; shown in tooltip
"default_popup": "popup.html" // optional
},
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": ["background.js"]
}
],
"manifest_version": 2
}
popup.html
<!doctype html>
<html>
<head>
<title>My Awesome Popup!</title>
<script>
function getPageandSelectedTextIndex()
{
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function (response)
{
console.log(response.farewell);
});
});
}
chrome.browserAction.onClicked.addListener(function(tab) {
getPageandSelectedTextIndex();
});
</script>
</head>
<body>
<button onclick="getPageandSelectedTextIndex()">
</button>
</body>
</html>
background.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
updateIcon();
});
function updateIcon() {
var allLinks = document.links;
for (var i=0; i<allLinks.length; i++) {
alllinks[i].style.backgroundColor='#ffff00';
}
}
Ban đầu tôi muốn đánh dấu tất cả các liên kết trên t anh ta trang hoặc đánh dấu chúng theo một cách nào đó; nhưng tôi nhận được lỗi "Từ chối thực thi tập lệnh nội tuyến vì Nội dung-Bảo mật-Chính sách".
Khi tôi nhấn nút bên trong cửa sổ bật lên, tôi gặp lỗi này: Refused to execute inline event handler because of Content-Security-Policy
.
Vui lòng giúp tôi khắc phục các lỗi này, vì vậy tôi có thể mở tất cả các liên kết trong tab mới bằng tiện ích mở rộng chrome của tôi.
Chỉ cần đánh tôi với nó. Câu trả lời hoàn hảo và chính xác là những gì bạn cần làm. – Alasdair
k thanks..i sẽ thử nó – Saad
Điều này thật ngu ngốc. Ai nảy ra ý tưởng này ?! Bây giờ tôi phải thay đổi tất cả onclicks nội tuyến của tôi và đặt chúng trong một tập tin js bên ngoài. Và tôi có một loạt các tập tin bao gồm cả chúng. –