Chrome扩展程序:点击可编辑当前网址,然后重定向到已修改的网址 [英] Chrome Extension: Edit the current url on click and then redirect to the edited one

查看:42
本文介绍了Chrome扩展程序:点击可编辑当前网址,然后重定向到已修改的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a psychology student and I read papers very often. The university libraries provide the access to the databases but I need to use library search engine and log in every time. Quite annoying. I found a way to avoid jumping around the pages.

这是方法:

在Google学术搜索中找到论文后,我在目标数据库地址的末尾添加了"ezp.lib.unimelb.edu.au",然后它将重定向到图书馆登录页面.

I add "ezp.lib.unimelb.edu.au" to the end of the target database address after I found a paper in Google Scholar, then it will redirect to the library login page.

例如,论文的地址是:

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

我将其修改为:

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

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

我想创建一个Chrome扩展程序以在单击时完成这项工作(太懒了).我试了几个小时,但没有用.

I want to create a Chrome Extension to finish this job on click (too lazy). I tried for hours but it does not work.

这是我所做的:

我在一个文件夹中有三个文件:

I have three files in a folder:

第一个文件:manifest.json

First file: 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"
  ]
}

第二个文件:popup.js

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


  });

}

第三文件:unimelb.png

Third file: unimelb.png

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

When I load this folder into Chrome, it does not work.

这是我第一次使用JS,有人对它有什么建议吗?

It's the first time I use JS, anyone has any suggestions?

谢谢!

推荐答案

即使不单击也可以执行此操作.您可以针对该网址格式使用内容脚本,以便将脚本注入到此页面.然后,您可以使用 chrome.runtime.sendMessage()将消息发送到后台脚本,并且侦听器将在此处创建您想要的链接,然后使用 chrome.tabs.update重新加载选项卡()和新的网址.

You can do this even without clicking. You can use the content script for this URL pattern so that your script gets injected to this page. Then you can send a message to the background script using chrome.runtime.sendMessage() and your listener will create a link you want here and then just reload the tab using chrome.tabs.update() with the new URL.

manifest.json

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

  "content_scripts": [{
      "matches": ["http://www.sciencedirect.com/science/article/pii/*"],
      "js": ["content-script.js"]
  }],
  ...
}

content-script.js

chrome.runtime.sendMessage({loadURL: true});

background.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})     
     }
);

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

This is my first answer to the StackOverflow Community, I hope it helps.

这篇关于Chrome扩展程序:点击可编辑当前网址,然后重定向到已修改的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆