[英]Script to remove part of img href on a site
I fired up a game guide on PrimaGames.com. 我在PrimaGames.com上发布了一个游戏指南。 The text is all fine but the images have busted src.
文字很好,但图像已经破坏了src。 Here's an example:
这是一个例子:
<img src="/media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png/PRIMAP/resize/700x-1>">
At the end of the src URL, you'll notice this odd string: 在src URL的末尾,您会注意到这个奇怪的字符串:
/PRIMAP/resize/700x-1> / PRIMAP /调整大小/ 700X-1>
I'm looking to set up a script (Stylish, Tampermonkey, etc) I can apply to PrimaGames.com so that it auto-removes that part of the src URL, which will in turn display the associated image. 我正在寻找一个脚本(Stylish,Tampermonkey等)我可以应用于PrimaGames.com,以便它自动删除src URL的那一部分,这将显示相关的图像。
Bookmarklet? 小书签?
javascript:(function() { [...document.images].forEach(img => {
const src = img.src.split("/PRIMAP");
if (src.length >=2) img.src=src[0]; })})()
Alternative for unknown stuff after the file extension - here assuming you are only interested in pngs 文件扩展名后的未知内容的替代方案 - 这里假设您只对png感兴趣
javascript:(function() { [...document.images].forEach(img => {
const end = img.src.lastIndexOf(".png/");
if (end) img.src=img.src.substring(0,end+4); })})()
This is a fairly standard src
rewrite task. 这是一个相当标准的
src
重写任务。
Here's a complete Tampermonkey / Violentmonkey script that should work on that site for both static and dynamic images : 这是一个完整的Tampermonkey / Violentmonkey脚本 ,可以在该站点上运行静态和动态图像 :
// ==UserScript==
// @name _Remove post file junk from image sources
// @match *://primagames.com/*
// @noframes
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// @grant GM.getValue
// ==/UserScript==
// @grant none
//- The @grant directives are needed to restore the proper sandbox.
/* global waitForKeyElements */
waitForKeyElements ("img", fixImgSource);
function fixImgSource (jNode) {
var oldSrc = jNode.attr ("src");
//-- Does the src have junk after the file suffix?
if (/\.(png|jpg|gif)./i.test (oldSrc) ) {
let newSrc = oldSrc.replace (/\.(png|jpg|gif).+$/i, ".$1");
jNode.attr ("src", newSrc);
}
}
You will need to find the index where the actual image ends and create a substring up to that index. 您需要找到实际图像结束的索引,并创建一个直到该索引的子字符串。
function removeTag(image) {
var src = image.src.toString();
var imgFormats = ['jpg', 'jpeg', 'gif', 'png'];
var idx;
imgFormats.forEach(fmt => {
if (src.lastIndexOf(fmt) > -1) {
idx = src.lastIndexOf(fmt) + fmt.length;
}
})
if (typeof idx !== 'undefined') {
image.src = src.substr(0, idx + 1);
}
}
If you know that every extra string will start with '/PRIMAP', then you can use the solution provided by above. 如果您知道每个额外的字符串都以'/ PRIMAP'开头,那么您可以使用上面提供的解决方案。
You just need to execute below code, Here I am just replave image src with image url, after replacing src 你只需要执行下面的代码,在这里我只是在替换src后用image url替换图像src
(function(){
var a = document.getElementsByTagName('img');
for(e of a){
let t = e.src;
e.src = t.replace(/(http|https|file:\/\/)?(\/.*\.(?:png|jpg))(.*)/i, '$2');
}
}())
Here I am changing: 我在这里改变:
/media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png/PRIMAP/resize/700x-1%3E /media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png/PRIMAP/resize/700x-1%3E
to 至
/media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png /media/files/eguide_assets/final-fantasy-xii-the-zodiac-age-eguide/085-105_walkthrough-web-resources/image/wi01.png
Then url taking File from relative path that is why I am using file path, 然后url从相对路径获取File,这就是我使用文件路径的原因,
If this image in you relative folder then you will get image, Else you need to add base url with the relative path 如果您在相关文件夹中的此图像,那么您将获得图像,否则您需要添加相对路径的基本URL
as https://primagames.com/ then relative path mean code will change to : 作为https://primagames.com/然后相对路径平均代码将更改为:
(function(){
var a = document.getElementsByTagName('img');
for(e of a){
let t = e.src;
e.src = `https://primagames.com${t.replace(/(http|https|file:\/\/)?(\/.*\.(?:png|jpg))(.*)/i, '$2')}`;
}
}());
It will work for you. 它会对你有用。
For Only remove extra stuff from image url, that is "/PRIMAP/resize/700x-1%3E", You can do below code 仅用于从图片网址中删除额外的内容,即“/ PRIMAP / resize / 700x-1%3E”,您可以执行以下代码
(function(){
var a = document.getElementsByTagName('img');
for(e of a){
let t = e.src;
e.src = t.replace(/([\w-]+\.(jpg|png|txt))(.*)/i, '$1');
}
}())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.