【发布时间】:2019-10-07 11:15:37
【问题描述】:
我不知道如何将 HTML 和 javascript 注入带有 Firefox 扩展的网页。
这适用于 Chrome 扩展,但不适用于 Firefox。请注意,我使用了 chrome.extension.getURL,它引入了 HTML 和 Javascript。
这是我的清单 - 注意我什至不使用背景的东西
{
"background": {
"scripts": [ "js/background.js", "js/jquery.js"]
},
"content_scripts": [ {
"js": [ "js/jquery.js", "js/chart-min.js", "js/chart-colors.js", "js/jquery-ui.min.js", "js/profiles/my_request.js", "js/options.js", "js/profiles/projects_builder.js", "js/profiles/board_builder.js", "js/profiles/my_api.js", "js/profiles/user_board_builder.js", "js/profiles/user_board.js","js/profiles/default_labels.js", "js/profiles/default_lists.js", "js/profiles/master_board.js", "js/profiles/projects.js", "js/profiles/estimates.js", "js/profiles/new_todo_label.js","js/profiles/reports_dashboard.js", "js/profiles/mutation_observer.js", "js/profiles/completion_chart.js", "js/profiles/cumulative_flow_chart.js" ],
"matches": [ "https://asana.com/*" ],
"all_frames": true
}],
"permissions":[ "identity", "cookies", "storage", "activeTab", "https://asana.com/*"],
"name": "Boards here",
"short_name" : "boards",
"version": "3.1.9.5",
"manifest_version": 2,
"icons" : { "48": "images/logo_thicker.png"},
"description": "html for website",
"browser_action":{
"default_icon":"images/logo_thicker.png",
"default_popup":"html/popup.html"
},
"web_accessible_resources": [ "images/*", "html/*" ]
}
默认列表示例 -- default_lists.js 利用 my_request,它只是一个 jquery ajax 包装器
DefaultLists = (function() {
function DefaultLists() {
if (window.location.href.includes('#default_lists')) {
this.show_form()
}
DefaultLists.prototype.show_form = function {
my_request.ajax({
url: chrome.extension.getURL("html/manage_default_lists.html"),
type: 'get',
success: function (data) {
$('.panel.panel--project').remove()
$('.panel.panel--perma').html(data)
}
});
};
}
return DefaultLists;
})();
window.default_lists = new DefaultLists();
所以现在 manage_default_lists.html 看起来像
<section style="position:relative;top:-50px;" class="message-board__content">
<bc-infinite-page page="1" reached-infinity="" direction="down" trigger="bottom">
<table class="my_labels" data-infinite-page-container="">
<tbody>
<tr id="loading_lists" >
<td>
<span style="margin-left:280px;color:grey">Loading...</span>
</td>
</tr>
<tr id="create_row" style="display:none">
<td>
<span class="">
<input id="new_label_name" placeholder="List name" type="text" style="width:180px;font-size:15px;margin-left:42px;padding:5px;border-radius: 0.4rem;border: 1px solid #bfbfbf;" value="">
<a style="margin-left:10px;float:right;margin-right:80px" class="cancel_new btn--small btn small" href="#">cancel</a>
<input id="create_label" style="float:right;" type="submit" value="save" class="btn btn--small btn--primary primary small" data-behavior="loading_on_submit primary_submit" data-loading-text="saving…">
</span>
</td>
</tr>
</tbody>
</table>
</bc-infinite-page>
</section>
</article>
<script>
$('#cancel_delete_label_button').on('click', function(event){
$('#delete_label_modal').hide()
});
$('#cancel_force_lists_button').on('click', function(event){
$('#force_lists_modal').hide()
});
$(document).off( "click", '.edit_label').on('click', '.edit_label', function(event) {
td = $(this).parents('td')
td.find('.show_row').hide()
td.find('.edit_row').show()
event.preventDefault()
});
$(document).off( "click", '.cancel_edit').on('click', '.cancel_edit', function(event) {
td = $(this).parents('td')
td.find('.show_row').show()
td.find('.edit_row').hide()
event.preventDefault()
});
$(document).off( "click", '.cancel_new').on('click', '.cancel_new', function(event) {
// console.log('cancel')
$('#create_row').hide()
event.preventDefault()
});
$(document).off( "click", '#new_label_button').on('click', '#new_label_button', function(event) {
$('#create_row').show()
$('#new_label_name').val('')
event.preventDefault()
});
$(document).off( "click", '#labels_article').on('click', '#labels_article', function(event) {
// console.log(event.target.className)
if (event.target.className != 'color-editor-bg'){
$('.label-colors').hide();
}
});
</script>
【问题讨论】:
-
Chrome 扩展程序还默认禁止在带有
chrome-extension://URL 的页面中使用内联脚本,这意味着弹出窗口或选项或背景页面。如果它对你有用,那么要么你没有在扩展页面上下文中运行它,要么你已经明确地放松了content_security_policy。从您在问题中提供的信息中可以推断出这一点。 -
嗯不知道它是如何工作的,这很可怕。我没有设置 content_security_policy。是否有我可以在清单中使用的政策来使其发挥作用?我还可以提供哪些其他有用的信息?谢谢!
-
请edit 成为主题的问题:包括一个minimal reproducible example 重复问题。对于 Chrome 扩展程序或 Firefox WebExtensions,您几乎总是需要包含您的 manifest.json 和一些背景、内容和/或弹出脚本/HTML,通常是网页 HTML/脚本。寻求调试帮助的问题(“为什么我的代码没有按我想要的方式工作?”)必须包括:(1)期望的行为,(2)特定的问题或错误以及(3)重现它所需的最短代码在问题本身中。另请参阅:What topics can I ask about here? 和 How to Ask。
-
@wOxxOm 新添加的信息有帮助吗?
标签: google-chrome-extension firefox-addon firefox-addon-webextensions