繁体   English   中英

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

[英]Chrome Extension: Edit the current url on click and then redirect to the edited one

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

这是方法:

我在谷歌学术找到一篇论文后在目标数据库地址末尾添加“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" ] }

第二个文件: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


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

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

谢谢!

制作书签可以在任何浏览器中使用都比创建扩展要容易得多。

  • 右键点击书签栏
  • 选择“添加页面...”
  • 在“名称”下,输入任何您想要的“日记重定向”或其他内容
  • 在“ URL”下,复制并粘贴以下代码(无空格)

     javascript:(function(){location.href=location.href.replace('sciencedirect.com/','sciencedirect.com/ezp.lib.unimelb.edu.au/');})(); 

现在,当您在页面上时,单击该书签,它将重定向您。


更新 :在其他域的URL中尝试此代码

javascript:(function(){var%20l=location;l.href=l.origin+l.href.replace(l.origin,'ezp.lib.unimelb.edu.au/');})(); 

您甚至无需单击即可执行此操作。 您可以将内容脚本用于此URL模式,以便将脚本注入到此页面。 然后,您可以使用chrome.runtime.sendMessage()将消息发送到后台脚本,并且侦听器将在此处创建您想要的链接,然后只需使用带有新URL的chrome.tabs.update()重新加载标签即可。

manifest.json

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

  "content_scripts": [{
      "matches": ["http://www.sciencedirect.com/science/article/pii/*"],
      "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社区的第一个答案,希望对您有所帮助。

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":["background.js"] }, "permissions": [ "activeTab", "tabs" ] } 


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


popup.js

 // Change the url to library when on click var l=location;l.href=l.origin+l.href.replace(l.origin, '.ezp.lib.unimelb.edu.au'); 

他们运作良好。

完成第一个chrome扩展程序非常酷。 感谢Mottie的帮助。

任何想要根据某种模式编辑 url 的人都可以使用Regex 的 Chrome 扩展 Edit Url

例如,对于本文中的场景,在使用扩展时,您可以提供正则表达式http.*/science/和值http://www.sciencedirect.com.ezp.lib.unimelb.edu.au/科学/然后点击提交。 url 将按预期更新。

暂无
暂无

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

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