简体   繁体   English

使用Firefox插件修改网站

[英]Modify websites with Firefox Add-on

I'm writing a Firefox Add-on that modifies some specific websites when I open them. 我正在写一个Firefox插件,当我打开某些网站时会对其进行修改。 But I don't like the API at all. 但是我一点都不喜欢API。 For example if I want to add a div-container that contains a button to a tab. 例如,如果要向选项卡添加包含按钮的div容器。 It would look about like this: 它看起来像这样:

var myScript = 'document.body.innerHTML += "<div style=background-color:rgba(0,0,0,0.5);position:fixed;width:300px;height:300px;right:0px;top:0px;><input type=button value=test></input></div>";';
tab.attach({ contentScript: myScript });

This is: 这是:
1. Completely unreadable. 1.完全不可读。
2. Will look different on lots of websites because some websites add default styles to all input-elements. 2.在许多网站上看起来会有所不同,因为某些网站会向所有输入元素添加默认样式。

So is there a more readable way to modify a website that doesn't require to add a script as a string? 那么,是否有一种更易读的方式修改网站,而无需将脚本添加为字符串?
And is there a way to overlay any element on the website (even flash)? 有没有一种方法可以覆盖网站上的任何元素(甚至包括Flash)?

You should use contentScriptFile . 您应该使用contentScriptFile

And then you should use DOM APIs that don't suck performance and "shoot yourself in the foot"-wise (that are not .innerHTML ), something like: 然后,您应该使用不会降低性能并“自行解决”的DOM API(不是.innerHTML ),例如:

var div = document.createElement("div");
div.style.backgroundColor = "rgba(0,0,0,0.5)";
...
var input = document.createElement("input");
input.setAttribute("type", "button");
...
div.appendChild(input);
document.body.appendChild(div);

MDN has the documentation. MDN具有文档。

As for styles: This one is tougher. 至于款式:这个比较难。 Usually people will not inject DOM nodes into the page itself (not just because of CSS compatibility, but to avoid conflicts in Javascript and/or the DOM), but instead create an <iframe> which then has it's own set of CSS rules and own Javascript environment and is even subject to the same origin policy, which will hinder the website to mess with the contents, if done right. 通常,人们不会将DOM节点注入到页面本身中(不仅仅是因为CSS兼容性,而是为了避免Javascript和/或DOM中的冲突),而是创建一个<iframe> ,然后它具有自己的CSS规则集和自己的Java脚本环境甚至受同一原始策略的约束,如果操作正确的话,这将阻止网站弄乱内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM