Skip to content

Commit

Permalink
fixed: fetch cover Referer
Browse files Browse the repository at this point in the history
  • Loading branch information
zhifengle committed Jul 15, 2021
1 parent 26ea419 commit b75d3d1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"tabs",
"storage",
"webRequest",
"webRequestBlocking",
"<all_urls>"
]
}
38 changes: 37 additions & 1 deletion src/bg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ interface Config {

let E_USER_CONFIG: Config = {};

// ref: header editor
const IS_CHROME = /Chrome\/(\d+)\.(\d+)/.test(navigator.userAgent);

async function sendMsgToCurrentTab(
payload: LogMsg & Record<string, string | number>
) {
const tabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
browser.tabs.sendMessage(tabs[0].id, payload);
if (tabs && Array.isArray(tabs) && tabs[0]) {
browser.tabs.sendMessage(tabs[0].id, payload);
}
}

async function handleMessage(request: ExtMsg) {
Expand Down Expand Up @@ -137,7 +142,9 @@ async function updateAuxData(payload: {
duration: 0,
});
console.info('the start of updating aux data');
window._fetch_url_bg = auxSite;
const auxData = await getWikiDataByURL(auxSite, auxSiteOpts);
window._fetch_url_bg = null;
const obj = await browser.storage.local.get(['wikiData']);
console.info('current wikiData: ', obj.wikiData);
if (!auxData || (auxData && auxData.length === 0)) {
Expand Down Expand Up @@ -186,6 +193,18 @@ async function updateAuxData(payload: {
}
}

function createHeaderListener(type: string): any {
const result = ['blocking'];
result.push(type);
if (
IS_CHROME &&
// @ts-ignore
chrome.webRequest.OnBeforeSendHeadersOptions.hasOwnProperty('EXTRA_HEADERS')
) {
result.push('extraHeaders');
}
return result;
}
async function init() {
// 初始化设置
const obj = await browser.storage.local.get(['version', 'config']);
Expand Down Expand Up @@ -213,6 +232,23 @@ async function init() {
console.log('E_CONFIG: ', E_USER_CONFIG);
}
});
browser.webRequest.onBeforeSendHeaders.addListener(
(obj) => {
let m = (obj?.url ?? '').match(/brandnew\/(\d+)/);
if (m) {
obj.requestHeaders.push({
name: 'Referer',
value: `http://www.getchu.com/soft.phtml?id=${m[1]}`,
});
}
return { requestHeaders: obj.requestHeaders };
},
{
// urls: ['http://*/brandnew/*'],
urls: ['http://www.getchu.com/brandnew/*'],
},
createHeaderListener('requestHeaders')
);
}

init();
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ declare var GM_getResourceText: any;
// @TODO avoid use global variable
interface Window {
_parsedEl: Element | Document;
// 后台的 url
_fetch_url_bg?: string;
}
3 changes: 3 additions & 0 deletions src/sites/erogamescape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export const erogamescapeTools: SiteTools = {
cookie: 'getchu_adalt_flag=getchu.com',
decode: 'EUC-JP',
},
prefs: {
targetNames: ['cover'],
},
},
},
};
Expand Down
9 changes: 9 additions & 0 deletions src/sites/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ export async function getCover($d: Element, site: ModelKey) {
}
if (!url) return;
try {
// 在其它网站上获取的相对路径的链接
// @TODO 这里临时使用的全局变量来处理
if (window._fetch_url_bg && !/^https?:/.test(url)) {
const urlObj = new URL(window._fetch_url_bg);
url = `${urlObj.origin}/${url.replace(/^\.?\/?/, '')}`;
}
// 跨域的图片不能用这种方式
// dataUrl = convertImgToBase64($d as any);
let opts: any = {};
if (site.includes('getchu')) {
opts.headers = {
Referer: location.href,
};
if (!location.href.includes('getchu.com') && window._fetch_url_bg) {
opts.headers.Referer = window._fetch_url_bg;
}
}
dataUrl = await getImageDataByURL(url, opts);
if (dataUrl) {
Expand Down
2 changes: 2 additions & 0 deletions src/user-script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ async function updateAuxData(payload: {
duration: 0,
});
console.info('the start of updating aux data');
window._fetch_url_bg = auxSite;
const auxData = await getWikiDataByURL(auxSite, auxSiteOpts);
window._fetch_url_bg = null;
if (!auxData || (auxData && auxData.length === 0)) {
logMessage({
type: 'error',
Expand Down
8 changes: 8 additions & 0 deletions src/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,11 @@ export function genAnonymousLinkText(url: string, text: string): string {
${text}</a>
`;
}

export function addHTMLBase(html: string, url: string): string {
if (html.match(/<base.+>/)) {
return html;
}
const obj = new URL(url);
return html.replace('</head>', `<base href="${obj.origin}"></head>`);
}

0 comments on commit b75d3d1

Please sign in to comment.