waitForKeyElements(); - Stop a script firing on a popup in Chrome Extension?

一个人想着一个人 提交于 2020-01-16 18:13:15

问题


This question is a follow-on to another question which needed asking and warranted a new post, so excuse me if I refer to things which may not be clear without reading the other question.

When using the utility waitForKeyElements() I'm facing an issue in which a div is included inside a small popup contained within the same URL. My extension is currently running on the Twitter site, and my intention is that a div contained on the profile pages (e.g. http://twitter.com/todayshow) gets moved above another div on the page. I'm doing this via waitForKeyElements() because of some loading issues which are resolved by using this utility.

However, on a profile page you can click a link to another users name which pops up a small window (inside the same window/tab, on the same URL) showing some info about them and a few previous tweets. The issue here is that the same div appears on this popup and is then moved to the main page behind the popup window, where it shouldn't be. On a profile page, this can be stopped by plugging in the false parameter to waitForKeyElements(), however on a non-profile page it is still possible to activate this popup which is then moving onto the main page, as the div I wish to move it above on a profile page still exists here, causing clear issues.

I'm wondering if there's a way around this, as bugs in Chrome have stopped me from excluding these pages. So far (just brainstorming) I'm thinking:

  • on a page where the div doesn't exist to begin with, create an empty one meaning false will handle the issue.
  • somehow stop the script from firing on a given URL, although due to the way Twitter works this would have to monitor OnClick() and the page URL (I think) which I'm unsure how to do.
  • stop running when the popup appears, but I have almost no idea where to start with that.

Any help is appreciated. Any necessary code related to this question can be found in the first two links, and the issue I'm facing can be seen by a quick visit to Twitter.

EDIT: When plugging in the false param it works when going directly to profiles via the URL bar, if you're on a profile and use a link to get to a profile, the script isn't inserted and my extension fails. So this would need resolving too, or an alternative method altogether.


回答1:


I had a brainwave that I could use insertAfter() to insert the <div> I was originally moving in front of, after the <div> I was originally moving. This <div> is not present on the popup, which means that nothing is moved onto the back page when it shouldn't be.

In regards to the previous question, my code is now simply:

waitForKeyElements (
    "jQuery selector for div(s) you want to move", // Opposite to what it was.
    moveSelectDivs
);

function moveSelectDivs (jNode) {
     jNode.insertAfter ("APPROPRIATE JQUERY SELECTOR"); // Again, the opposite.
}

This solves the issue I was having and my extension is now working just fine, however I will leave this question posted in case anybody comes back to it in future.



来源:https://stackoverflow.com/questions/14227689/waitforkeyelements-stop-a-script-firing-on-a-popup-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!