简体   繁体   English

Chrome扩展程序-jQuery关闭模式弹出窗口

[英]Chrome extension - jquery closing modal popup window

I have made an extension for chrome. 我已经扩展了chrome。 It's purpose is to read data from popup window, store that data to database and then close the popup window. 目的是从弹出窗口读取数据,将数据存储到数据库,然后关闭弹出窗口。 Everything is working great but I cannot close the popup window. 一切正常,但是我无法关闭弹出窗口。

My plan is to close the popup window with this code: 我的计划是使用以下代码关闭弹出窗口:

$('a.tv-dialog-title-close').trigger('click'); 

if I type that in to chrome console it closes the popup window nicely, but when I use that exact code in my extension then popup window wont close and I get no errors (weird). 如果我在chrome控制台中键入该命令,它将很好地关闭弹出窗口,但是当我在扩展程序中使用该确切代码时,弹出窗口将不会关闭,并且我不会收到任何错误(奇怪)。 Everything else is working just as planned but this one line of code is not working. 其他所有内容均按计划工作,但这一行代码不起作用。 Anyone, any thoughts ? 有人,有什么想法吗?

Thank you 谢谢

EDIT: 编辑:

Got it working with this: 得到了它的工作:

var scriptNode          = document.createElement ('script');
scriptNode.textContent  = "$('a.tv-dialog-title-close').trigger('click');";
document.body.appendChild (scriptNode); 

Thank you Xan 谢谢X

When you execute this code inside the console, you execute it in the page's context. 在控制台内执行此代码时,将在页面的上下文中执行它。

When you execute this as a content script, it's executed in the isolated context and cannot reach page-defined handlers. 当您将其作为内容脚本执行时,它将在隔离的上下文中执行,并且无法访问页面定义的处理程序。 There is no error, as it's a valid call and triggers (non-existing) handlers registered by you. 没有错误,因为它是有效的调用,并且会触发您注册的(不存在的)处理程序。

Look into injecting your code into the page's context, as described here . 考虑注入代码到页面的背景下,作为这里描述

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

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