简体   繁体   English

如何在网址中获取特定参数并将其添加到Chrome扩展程序中的另一个网址?

[英]How to get a specific parameter in an url and add that to another url in Chrome Extension?

Only learnt coding for 2 weeks so sorry for noob questions! 仅学习了2周的编码,因此对菜鸟提出的问题感到抱歉!

I am trying to build a chrome extension that runs the following when I click a button on popup.html: 我试图构建一个Chrome扩展程序,当我在popup.html上单击一个按钮时,该扩展程序将运行以下命令:

  1. gets and reads the urls of each tab one by one 逐个获取和读取每个选项卡的URL
  2. if the url starts with " https://www.linkedin.com/company/ ", gets the company ID right after "company/". 如果网址以“ https://www.linkedin.com/company/ ”开头,则会在“ company /”之后获取公司ID。 (The url usually looks like this: https://www.linkedin.com/company/164233?trk=prof-exp-company-name so in this case for example, I would like to get "164233") Then it should create a new tab with the following url: https://www.linkedin.com/vsearch/p?f_CC=164233 (URL通常看起来像这样: https ://www.linkedin.com/company/164233?trk=prof-exp-company-name,因此在这种情况下,例如,我想获取“ 164233”)使用以下网址创建一个新标签页: https : //www.linkedin.com/vsearch/p? f_CC =164233

So far here are the codes I have for this button: 到目前为止,这里是此按钮的代码:

document.addEventListener('DOMContentLoaded', function (){
    document.getElementById('btn2').addEventListener('click', loadSeeAllEmployees);
});

function loadSeeAllEmployees(e){
    chrome.tabs.query({'active': true, 'lastFocusedWindow': true, 'currentWindow': true}, function (tabs) {
    for (i=0; i<chrome.tabs.getAllInWindow().length; i++){
        if(tabs.url.indexOf("https://www.linkedin.com/company/")){

        }
    }
};

}

So the name of the button is 'btn2' and the function is 'loadSeeAllEmployees' 因此,按钮的名称为“ btn2”,函数为“ loadSeeAllEmployees”

I am stuck here because I don't know how to get that specific parameter and how I can add it to another url. 我被困在这里是因为我不知道如何获取该特定参数以及如何将其添加到另一个URL。

Thank you in advance. 先感谢您。

String.prototype.slice() String.prototype.slice()

MDN MDN

The slice method extracts a section of a string and returns a new string. slice方法提取字符串的一部分并返回新的字符串。


If I am not misunderstanding the question, using the slice function will get what you need, assuming the URL format is consistent. 如果我对这个问题没有误解,那么假设URL格式是一致的,那么使用slice函数将满足您的需求。

This probably isn't the most reliable method, but: 这可能不是最可靠的方法,但是:

 var query = tabs[0].url; var companyId = query.slice(32, 38); 

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

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