簡體   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