我正在Twitter页面上运行tampermonkey脚本,该脚本会注入CSS并发出ajax请求。
Twitter返回content-security-policy
标头,该标头破坏了脚本的执行。解决方法是,我要删除带有Chrome扩展程序的标头,但不方便。
我想知道我是否可以通过tampermonkey本身删除响应头?
答案 0 :(得分:0)
我没有检查是否可以使用这种方法修改标题(我可以修改的响应正文)
但请尝试将https://github.com/jpillora/xhook/与Tampermonkey一起使用
class FoodGuide extends Component {
state = {
names: [],
suggestion: null,
getList: false,
loading: false,
error: false,
changed: false,
value: "2",
list: []
}
componentDidMount() {
const config = { headers: {'user-key': 'APIKEY'} };
axios.get(`/search?entity_id=2891&entity_type=city&count=50&sort=rating$&cuisines=${this.props.value}` , config)
.then(res => {
this.setState({names: res.data.restaurants})
console.log(this.state.names)
})
.catch(error => {
this.setState({error: true})
})
}
getSuggestion = () => {
let rSuggestion;
if(this.state.names) {
let randomR = this.state.names[Math.floor(Math.random()*this.state.names.length)];
rSuggestion = randomR.restaurant.name
//console.log(randomR.restaurant.name)
this.setState({suggestion: rSuggestion})
}
return rSuggestion
}
getRestaurantList = () => {
let rData = <p>This is a suggestion</p>
if(this.state.getList) {
rData = this.state.names.map((r, i) => {
return(
<li key={i}>
<span>Name: { r.restaurant.name }</span>
</li>
)
})
}
return rData
}
getRestaurantsHandler = () => {
let rList = <p>Loading...</p>
if(this.state.names) {
this.setState({getList: true})
}
return rList
}
render () {
return (
<Aux>
<h2>Food Guide</h2>
<Cuisines />
<Suggestion suggested={ this.getSuggestion } suggestion={this.state.suggestion}/>
<RestaurantList getList={ this.getRestaurantsHandler } rList={ this.getRestaurantList() }/>
</Aux>
)
}