Chrome 扩展:点击编辑当前网址,然后重定向到编辑后的网址

Sat*_*urn 4 javascript jquery google-chrome google-chrome-extension

我是一名心理学学生,我经常阅读论文。大学图书馆提供数据库的访问,但我每次都需要使用图书馆搜索引擎并登录。很烦人。我找到了一种避免跳转页面的方法。

方法如下:

我在Google Scholar中找到一篇论文后,在目标数据库地址末尾添加“ezp.lib.unimelb.edu.au”,然后它会重定向到图书馆登录页面。

例如,论文地址是:

http://www.sciencedirect.com/science/article/pii/S0006899315008550

我将其修改为:

http://www.sciencedirect.com.ezp.lib.unimelb.edu.au/science/article/pii/S000689315008550

我想创建一个 Chrome 扩展程序来完成这项工作(太懒了)。我尝试了几个小时但没有成功。


这是我所做的:

我的文件夹中有三个文件:

第一个文件:manifest.json

{
  "manifest_version": 2,

  "name": "Damn! Take me to the library!",
  "description": "This extension automatically adds the 'ezp.lib.unimelb.edu.au' to the browser's address, allowing you to visit the databases bought by the library quickly",
  "version": "1.0",

  "browser_action": {
    "default_icon": "unimelb.png",
    "default_title": "Damn! Take me to the library!"
  },
  
  "background":{
    "scripts":["popup.js"]
  },

  "permissions": [
    "activeTab",
    "tabs"
  ]
}
Run Code Online (Sandbox Code Playgroud)

第二个文件:popup.js

function getCurrentTabUrlthenChangeIt(callback) {
  var queryInfo = {
    active: true,
    currentWindow: true
  };

  chrome.tabs.query(queryInfo, function(tabs) {
    
    var tab = tabs[0];

    var url = tab.url;

    callback(url);

    var newurl = url.replace('/',"ezp.lib.unimelb.edu.au/");

    window.location.replace(newurl);


  });

}
Run Code Online (Sandbox Code Playgroud)

第三个文件:unimelb.png


当我将此文件夹加载到 Chrome 中时,它不起作用。

第一次用JS,请问大家有什么建议吗?

谢谢!

Nik*_*rma 5

即使不点击也可以执行此操作。您可以使用此 URL 模式的内容脚本,以便将您的脚本注入到此页面。然后,您可以使用以下命令向后台脚本发送消息chrome.runtime.sendMessage(),您的侦听器将在此处创建您想要的链接,然后只需使用chrome.tabs.update()新 URL 重新加载选项卡。

清单.json

{
 "name": "My extension",
 ...

  "content_scripts": [{
      "matches": ["http://www.sciencedirect.com/science/article/pii/*"],
      "js": ["content-script.js"]
  }],
  ...
}
Run Code Online (Sandbox Code Playgroud)

内容脚本.js

chrome.runtime.sendMessage({loadURL: true});
Run Code Online (Sandbox Code Playgroud)

背景.js

chrome.runtime.onMessage.addListener(function(message, sender, response) {
     if (message.loadURL) {
         var newurl = sender.tab.url.replace("/", "ezp.lib.unimelb.edu.au/");
         chrome.tabs.update(sender.tab.id, {url: newURL})     
     }
);
Run Code Online (Sandbox Code Playgroud)

这是我在 StackOverflow 社区的第一个回答,希望对您有所帮助。