在Chrome扩展程序中重定向后获取结束URL

时间:2022-10-20 09:50:26

First off, I'm new to making chrome extensions, so don't assume I know a whole lot. For the extension I'm making the user needs to be able to right click on a link, select a context menu item, and the extension needs to get sent the final url of that link.

首先,我是制作镀铬扩展的新手,所以不要以为我知道很多。对于扩展,我正在使用户需要能够右键单击链接,选择上下文菜单项,并且扩展需要发送该链接的最终URL。

Specifically amazon affiliate links. So for example the following:

特别是亚马逊联盟链接。例如,以下内容:

http://amzn.to/1VO7dlp

Would need to get converted to:

需要转换为:

http://www.amazon.com/gp/product/0470281731/ref=as_li_ss_tl?ie=UTF8&fpl=fresh&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-1&pf_rd_r=0WRZW1V7VVDJWS54WCM4&..... blah blah blah

HTTP://www.amazon.com/gp/product/0470281731/ref=as_li_ss_tl即= UTF8&FPL =新鲜pf_rd_m = ATVPDKIKX0DER&pf_rd_s =桌面-1 pf_rd_r = 0WRZW1V7VVDJWS54WCM4&......等等等等等等

I've looked around and I can't find any answers. Am I SOL?

我环顾四周,找不到任何答案。我是SOL吗?

The code I have so far is pretty basic:

我到目前为止的代码非常基本:

//background.js

chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
    title: 'Add this Link',
    id: 'linkContext',
    contexts: ['link'],
 });
}); 

chrome.contextMenus.onClicked.addListener(function(data, tab) {
    if (data.menuItemId === "linkContext") { 
      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, 
          {
            linkUrl: data.linkUrl,
          }, 

          function(response) {
            alert(response.host);
          });
      });
    }
});


chrome.runtime.onMessage.addListener(

//content_script.js

function(request, sender, sendResponse) {
    if (request.linkUrl){

        pathArray = request.linkUrl.split( '/' );
        protocol = pathArray[0];
        host = pathArray[2];
        url = protocol + '//' + host;

        sendResponse({host: host});
    }
});

//manifest.json

{
  "name": "jQuery DOM",
  "manifest_version": 2,
  "version": "1.0",
  "description": "Manipulate the DOM when the page is done loading",
  "browser_action": {
    "name": "Manipulate DOM",
    "icons": ["icon.png"],
    "default_icon": "icon.png"
  },

  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },

  "permissions": [
    "contextMenus"
  ],

  "content_scripts": [ {
    "js": [ "jquery.min.js", "content_script.js" ],
    "matches": [ "http://*/*", "https://*/*"]
  }],

  "web_accessible_resources":[
    "menu.html",
    "menu.css"
  ]
}

Like I said I'm pretty new to this, so I'm unsure of how to proceed. I'd like to do some parsing of the "final url" so I can present information about it to the user. I.E. the Affiliate ID. But for that I can't use the shortened link from above.

就像我说的那样,我对此很新,所以我不确定如何继续。我想对“最终网址”进行一些解析,以便我可以向用户提供有关它的信息。 I.E.会员ID。但为此我不能使用上面的缩短链接。

1 个解决方案

#1


0  

As Redirects is a response from the server back to you, you can read the header-field and see what url your initial url redirects to.

由于重定向是服务器返回给您的响应,您可以阅读标题字段并查看您的初始网址重定向到的网址。

In this case, I just tested to open a new tab in Chrome, and opened the Network tab and navigated to your initial url. Then I saw two separate redirects (http status code 301), before the the full url of the resulting page was shown.

在这种情况下,我刚测试在Chrome中打开一个新标签页,然后打开“网络”标签并导航到您的初始网址。然后,在显示结果页面的完整URL之前,我看到了两个单独的重定向(http状态代码301)。

This you can do in code as well, to be able to get to the final url. :)

您也可以在代码中执行此操作,以便能够访问最终的URL。 :)

#1


0  

As Redirects is a response from the server back to you, you can read the header-field and see what url your initial url redirects to.

由于重定向是服务器返回给您的响应,您可以阅读标题字段并查看您的初始网址重定向到的网址。

In this case, I just tested to open a new tab in Chrome, and opened the Network tab and navigated to your initial url. Then I saw two separate redirects (http status code 301), before the the full url of the resulting page was shown.

在这种情况下,我刚测试在Chrome中打开一个新标签页,然后打开“网络”标签并导航到您的初始网址。然后,在显示结果页面的完整URL之前,我看到了两个单独的重定向(http状态代码301)。

This you can do in code as well, to be able to get to the final url. :)

您也可以在代码中执行此操作,以便能够访问最终的URL。 :)