繁体   English   中英

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

[英]Get end URL after redirect in chrome extension

首先,我不熟悉chrome扩展,所以不要以为我了解很多。 对于扩展程序,我要使用户需要能够右键单击链接,选择上下文菜单项,然后需要向该扩展程序发送该链接的最终 URL。

特别是亚马逊会员链接。 因此,例如以下内容:

http://amzn.to/1VO7dlp

需要转换为:

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

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

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

//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"
  ]
}

就像我说的那样,我还很陌生,所以我不确定该如何进行。 我想对“最终网址”进行一些解析,以便向用户展示有关其的信息。 IE会员ID。 但是为此,我不能使用上面的缩短链接。

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

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

您也可以在代码中执行此操作,以获取最终的URL。 :)

暂无
暂无

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

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