简体   繁体   English

如何在不重新加载页面的情况下从 FF web 扩展 content_script 更改第三方网站上的 angular 应用程序路由/URL

[英]How to change angular app route/URL on a 3rd party website from a FF web extension content_script without reloading page

I have built a FF extension that loads a bunch of stock market symbols from "a.com".我已经构建了一个 FF 扩展,可以从“a.com”加载一堆股票市场符号。

Then in my FF browser the extension needs to programaticaly change the currently loaded route in site "b.com" which uses angularJS.然后在我的 FF 浏览器中,扩展程序需要以编程方式更改站点“b.com”中当前加载的路由,该站点使用 angularJS。

I do not own nor have access to change the code on site "b.com", except via the FF extension.我不拥有也无权更改站点“b.com”上的代码,除非通过 FF 扩展。 My extension uses jQuery and vanilla JS.我的扩展使用 jQuery 和 vanilla JS。

HINT: On b.com when I search for a stock symbol in a search input field (represented here by the CSS selector ".symbol-lookup-field input") then hit enter, it changes the route and its fast.提示:在 b.com 上,当我在搜索输入字段(此处由 CSS 选择器“.symbol-lookup-field input”表示)中搜索股票代码时,然后按回车键,它会更改路线并快速更改路线。

ATTEMPT #1: I can change the current route obviously by simply changing the whole window URL like this: window.location.href = 'https://b.com/trading/quote/A/chart';尝试#1:我可以通过简单地改变整个 window URL 来明显改变当前路线: window.location.href = 'trahttps://t';e/

However this takes forever to load and since I need to browse a hundred of these URLs I need this to be fast enough.然而,这需要永远加载,因为我需要浏览一百个这样的 URL,所以我需要它足够快。

ATTEMPT 2: So I tried to simulate an enter keypress/keydown event (tried both) and they fire correctly from a jQuery point of view when I setup an event listener for ENTER key, however it does not end up performing the same action as if I were to hit enter on my keyboard.尝试 2:所以我尝试模拟 enter keypress /keydown 事件(都尝试过),当我为 ENTER 键设置事件侦听器时,它们从 jQuery 的角度正确触发,但是它最终不会执行相同的操作,就好像我要按键盘上的回车键。 So that solution ended up not working for me.所以这个解决方案最终对我不起作用。 Note that the search field gets a green BG color so its not a CSS selector problem.请注意,搜索字段的 BG 颜色为绿色,因此它不是 CSS 选择器问题。

Here is my code for that part:这是我的那部分代码:

var myKeyDown = function(event){
    if ((event.keyCode) == 13) {
        alert('Enter keydown triggered');
        $(this).off('keydown',myKeyDown);
    }
};
$(".symbol-lookup-field input").off('keydown',myKeyDown).on('keydown',myKeyDown);
var e = jQuery.Event("keydown");
e.which = 13;
e.keyCode = 13;
$(".symbol-lookup-field input").css("background-color",'#e2efd5');
$(".symbol-lookup-field input").trigger(e).trigger("blur");

ATTEMPT #3: The other solution is to find a way to change the route externally from oustide an angular app that is already loaded.尝试#3:另一种解决方案是找到一种方法来从外部更改路线,从已经加载的 angular 应用程序中移除。 Does anyone knows a way to change the current route of an AngularJS App from a FF extension content_script?有谁知道如何从 FF 扩展 content_script 更改 AngularJS 应用程序的当前路线?

So for now Im stuck as none of the 3 solutions work good enough for needed use.所以现在我卡住了,因为这 3 种解决方案都不能满足需要的使用。 Anybody has a clue what to type to change the angular app route?任何人都知道输入什么来更改 angular 应用程序路线?

NOTES:笔记:

  • b.com website uses angular v1.5.8. b.com网站使用angular v1.5.8。
  • I can access the document & window object from the FF extension.我可以从 FF 扩展访问文档 & window object。
  • b.com uses directive/ngApp b.com 使用指令/ngApp

I finally found a solution.我终于找到了解决方案。

Since we can push history items on any browser, I used this with the last part being the URL I need the angular app to display:由于我们可以在任何浏览器上推送历史记录项目,我使用它的最后一部分是 URL 我需要 angular 应用程序来显示:

window.history.pushState({},null, '/trading/quote/A/chart');

in order to change the URL in the browser.为了在浏览器中更改 URL。 Because $location module/service in angular watches for url changes "live", the change is being picked up inside the angular app and is reflected by what is being displayed properly.由于 angular 中的 $location 模块/服务监视 url 更改“实时”,因此更改正在 angular 应用程序中获取,并通过正确显示的内容反映出来。

That solves the problem.这样就解决了问题。

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

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