简体   繁体   中英

Chrome Extension:redirect the current url with specific rules

I am a researcher and I want to access academic papers via my university. I discovered the I can modify the URL to achieve this. The rules are replacing "." with "-" and adding ".proxy.findit.dtu.dk" to the domain.

For example: From: https://www.sciencedirect.com/science/article/pii/S0925838811021414
to: https://www-sciencedirect-com.proxy.findit.dtu.dk/science/article/pii/S0925838811021414

Inspired by this post , I modified the code to:
First file: 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" ] }

Second file: 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');

Third file: 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..."); }); })

The last file I included is a figure: DTU.png.
However, it does not work. There is some problem with popup.js. I cannot replace "." with "-" and adding ".proxy.findit.dtu.dk" to the domain at the same time . Only the adding is working. What I got after running the example was: https://www.sciencedirect.com.proxy.findit.dtu.dk/science/article/abs/pii/S0925838811021414

I am totally new at JavaScript. Any suggestions to solve this?
Cheers!

Assigning to location.href is tricky because it leads to navigation so you should do it in one operation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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