简体   繁体   English

Chrome扩展程序:将javascript注入网页以执行ajax请求?

[英]Chrome extension: Inject javascript into a webpage to perform a ajax request?

I'm developing a Google Chrome extension that adds a Windows Live Messenger-like notification when a user logs on Facebook and thus I need to perform a ajax request to grab some user information (like profile picture, full name from id etc.) So since I can't perform a ajax request to Facebook directly (Correct me if I'm wrong), is it possible to inject a Javascript file into the facebook page and then execute the ajax request from there? 我正在开发一个Google Chrome扩展程序,该扩展程序在用户登录Facebook时添加类似于Windows Live Messenger的通知,因此我需要执行ajax请求以获取一些用户信息(例如个人资料图片,id中的全名等)。由于我无法直接向Facebook执行Ajax请求(如果我错了,请纠正我),是否可以将Javascript文件注入Facebook页面,然后从那里执行Ajax请求? Or will this be blocked since the extensions run in another environment (Or do they? Once again correct me if I misunderstood this!) I don't have any code to show at the moment, but I was just wondering if it's possible before I start the coding or if I need some other way around this. 还是因为扩展程序在另一个环境中运行而被阻止(或者它们是扩展程序?如果我误解了,请再次纠正我!)我目前没有任何代码可显示,但是我只是想知道在我之前是否有可能开始编码,或者如果我需要其他解决方法。

Thanks in advance 提前致谢

To inject it to head, you can write a function for it 要将其注入头部,可以为其编写函数

function exec(fn) {
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = '(' + fn + ')();';
    document.body.appendChild(script); // run the script
    document.body.removeChild(script); // clean up
}

and use it in your extension like 并在您的扩展程序中使用它

exec(function() {
    $("body").load('hacked.html');
});

taken from this answer 取自这个答案

Chrome extensions are allowed to make ajax requests to any page. Chrome扩展程序可以向任何页面发出Ajax请求。 See docs for details. 有关详细信息,请参阅文档

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

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