从iframe中的远程站点检测代码是否作为Chrome扩展运行
Detect that code is running as a Chrome extension, from a remote site, within iframe
问题:是否可以检测远程站点上运行的js代码是否从Chrome扩展中的iframe中调用?我有一个远程网站,当被调用为Chrome扩展时,我想覆盖一些行为,而是使用Chrome.runtime.sendMessage和Chrome.runtime.onMessageExternal将命令发送回扩展。
在我的popup.html中,我有这样的:
<iframe src="http://localhost" width="100%" height="100%" frameborder="0"></iframe>
在远程站点(用于测试的localhost)上的js中,我有以下内容:
$(document).ready(function () {
if (someHowCheckIfIAmBeingCalledFromAChromeExtension == true) { //what do I put here?
{
// The ID of the extension we want to talk to.
var extensionId = "abc";
// Make a simple request:
chrome.runtime.sendMessage(extensionId, { message: "connected!" },
function (response) {
if (!response.success)
handleError(message);
});
}
}
});
在我的popup.js中,我有:
chrome.runtime.onMessageExternal.addListener(
function (request, sender, sendResponse) {
//do something with request.message
});
这有可能吗?我看了很多其他的文章和问题,这些文章和问题都在谈论我想做什么,但并没有让我走上正确的道路。
谢谢!
我通过以下方式解决了这个问题:
- 将popup.html中的iframe指向localhost/index?延伸=铬
-
将以下jquery添加到远程/本地主机站点的索引中:
$(document).ready(function () { //check if query string is present if (getParameterByName('extension') == 'chrome') { var extensionId = "abc"; //append the query string of each link on the page to make sure the site stays in "extension mode" $('a').each(function () { var _href = $(this).attr('href'); var _newHref; if (_href.indexOf('?') >= 0) { _newHref = _href + '&extension=chrome'; } else { _newHref = _href + '?extension=chrome'; } $(this).attr("href", _newHref); }); //after this line I catch/change/override any behavior I want to change when the site is used form within the extension //Then, I use chrome.runtime.sendMessage to send any custom commands to the extension } });
-
然后,在popup.js中,我必须执行以下操作:
chrome.runtime.onMessageExternal.addListener( function (request, sender, sendResponse) { //do something with the caught behavior }); //If you want to make the website snap out of "extension mode", //clean all urls with this function when forwarding urls/opening tabs from the extension. //The website will then function as usual, stripped of all the extension specific behaviour. function cleanChromeExtensionQueryString(url) { var cleaned; if (url.indexOf('?extension=chrome') >= 0) { cleaned = url.replace('?extension=chrome', ''); } else if (url.indexOf('&extension=chrome') >= 0) { cleaned = url.replace('&extension=chrome', ''); }; return cleaned; }
这种方法允许我将响应式网站重新用作Chrome扩展,同时仍然可以修改某些行为,使其变得更"扩展式":-)。
当然,这种总体方法并不适用于所有场景,但它恰好适用于我当前的项目。在其他情况下,您最好从头开始创建扩展。
相关文章:
- 是否存在React Native“;WEB代码安全防护”;
- 如何检查这个代码点火器php函数是否工作,该函数是否在ajax url中使用
- 调试一个简单的jQuery函数;想知道是否与其他代码冲突
- 是否有一种方法可以使用公共代码库在Java和JavaScript中进行输入验证
- 是否可以在JavaScript中包含HTML代码
- 检查用户是否具有角色分析云代码
- 是否可以将此javascript代码(或其中的一部分)移动到单独的文件中,而不是内联
- Javascript:是否可以读取外部脚本的原始代码
- JS代码有什么问题,让我知道代码是否可以改进
- 确定 JavaScript 代码是否为表达式
- 是否有脚本/代码可以一次将链接属性添加到多个链接
- 是否可以将类添加到解析云代码中
- 是否有可能在 WebBrowser 控件中调试 JavaScript 代码
- 如何检查Firefox是否使用asm.js代码
- 是否有一个Javascript函数可以使代码在运行时延迟
- 是否可以向用户发送javascript代码's浏览器安全
- 我需要让用户输入他的出生日期,并使用javascript检查其形式是否正确.我的代码不起作用
- PHP是否以与JavaScript相同的方式解释代码
- adal.js是否支持授权代码授予流
- jQuery-用于检查是否有任何字段不为空,然后将所有字段设为必需字段的代码