【问题标题】:Chrome Extension:redirect the current url with specific rulesChrome 扩展:使用特定规则重定向当前 url
【发布时间】:2020-08-25 08:23:17
【问题描述】:

我是一名研究人员,我想通过我的大学访问学术论文。我发现我可以修改 URL 来实现这一点。规则正在替换“。”使用“-”并将“.proxy.findit.dtu.dk”添加到域中。

例如: 来自:https://www.sciencedirect.com/science/article/pii/S0925838811021414
至:https://www-sciencedirect-com.proxy.findit.dtu.dk/science/article/pii/S0925838811021414

this post的启发,我将代码修改为:
第一个文件:manifest.json

{
  "manifest_version": 2,

  "name": "Redirect via DTU",
  "description": "This extension automatically replace '.' with '-' and adds the '.findit.dtu.dk' to the browser's address, allowing you to visit the databases bought by the library quickly",
  "version": "1.0",

  "browser_action": {
    "default_icon": "DTU.png",
    "default_title": "Redirect via DTU!"
  },
  
  "background":{
    "scripts":["background.js"]
  },

  "permissions": [
    "activeTab",
    "tabs"
  ]
}

第二个文件:popup.js

// Change the url to library when on click
var l=location;
l.href=l.href.replace(/\./g, "-")
l.href=l.origin+l.href.replace(l.origin, '.proxy.findit.dtu.dk');

第三个文件:background.js

//Wait for click

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null, {
  	"file": "popup.js"
  }, function(){ 
  	"popup.js";
  	console.log("Script Executed ...");
  });
})

我包含的最后一个文件是一个图形:DTU.png。
但是,它不起作用。 popup.js 有一些问题。 我无法替换“。”使用“-”并同时将“.proxy.findit.dtu.dk”添加到域中。只有添加有效。运行示例后得到的是:https://www.sciencedirect.com.proxy.findit.dtu.dk/science/article/abs/pii/S0925838811021414

我对 JavaScript 完全陌生。有什么建议可以解决这个问题吗?
干杯!

【问题讨论】:

    标签: javascript google-chrome url redirect google-chrome-extension


    【解决方案1】:

    分配给location.href 很棘手,因为它会导致导航,所以您应该在一个操作中完成:

    location.href = 'https://' +
      location.hostname.replace(/\./g, '-') +
      '.proxy.findit.dtu.dk' +
      location.href.slice(location.origin.length);
    

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      相关资源
      最近更新 更多