我不知道如何将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/*" ]
}
默认列表示例-利用my_request的default_lists.js只是一个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>
您正在使用内容脚本和web_accessible_resources将此HTML添加到网页中。它与扩展程序的CSP无关,后者禁止扩展程序页面中的内联脚本,如browser_action弹出窗口或选项页面。在您的情况下,页面的CSP适用于您在其中添加的内容。该页面可能很容易禁止内联脚本,站点经常这样做。
您既可以使用webRequest API重写页面的Content-Security-Policy
HTTP标头,也可以稍微修改代码,后者是更好的解决方案,不仅因为它更加集中,而且还因为重写了HTTP的结果当多个扩展在同一HTTP请求中执行时,标头是随机的。
所以让我们重新编写代码:
行为的重要变化是,代码将使用内容脚本的jQuery和变量在内容脚本的上下文中运行。以前,您的代码在页面上下文中运行,因此它使用的是jQuery和页面的其他变量。
我正在使用Mozilla's browser
WebExtension namespace polyfill和异步/等待语法。
manifest.json:
browser
目录结构:
由于executeScript只能在扩展页面中使用,而不能在内容脚本中使用,所以我们将通过一条指令将消息发送到后台脚本。
content.js:
"background": {
"scripts": [
"browser-polyfill.min.js",
"background.js"
]
},
"content_scripts": [{
"js": [
"browser-polyfill.min.js",
"content.js"
],
"matches": ["........."]
}],
background.js:
async function getResource(name) {
const htmlUrl = browser.runtime.getURL(`html/${name}.html`);
const [html, scriptId] = await Promise.all([
fetch(htmlUrl).then(r => r.text()),
browser.runtime.sendMessage({
cmd: 'executeScript',
path: `html/${name}.js`,
}),
]);
const js = window[scriptId];
delete window[scriptId];
return {html, js};
}
DefaultLists.prototype.show_form = async () => {
const {html, js} = await getResource('manage_default_lists');
$('.panel.panel--project').remove()
$('.panel.panel--perma').html(html);
js();
};